PAGETURNER
ENCORE5
PAUSE
UTERMINAL
TERMINAL
NEXT
$V1$
SOMECLEAN
ENCORE1
ENCORE
BOX
SOLUTION
GO
Let's look at this problem together!
Step 1 is to establish a clear goal.
The primary goal is to: Write an algorithm for adding an
entry into a linked list of various variables!
The secondary goal is to:
Learn how to use a linked list of structures
in conjunction with unions!
GIVEN: a variable in the list may be one of the following types:
char, int, float, double!
A data structure that can hold any one of these
variables at a time is called _________.
Answer here:
That is correct!
You've got it on the 2nd try.
Good , now you understand the concept!
It is a heterogeneous data structure!
It is similar to a structure.
Please review Topic 4: The Union Concept
FORGET1
The correct answer is union.
Thus, we may have a union definition like the one above.
union union_tag {
char typ_char;
int typ_int;
float typ_float;
double typ_double;
};
It is also necessary that all the entries
be arranged in a linked list, as shown above.
<---[-] A <---[-]14 <---[-]2.15 <---[-]A
The data structure that can contain a pointer to itself is
called ________.
Answer here:
That is correct!
You've got it on the 2nd try.
Good, now you understand the concept!
It is a heterogeneous data structure!
It can hold various types at the same time.
Please review Lesson 9, Topic 2: Structure Definition and Declaration
FORGET2
The correct answer is struct or structure!
Therefore, we may have another data structure declaration:
struct uni_struct {
union union_tag uni_var;
struct uni_struct *next_union;
};
Thus, the idea is to add one entry at a time. We'll call the
function that will do that: add_entry(). It will return
a pointer to the structure of type uni_struct.
struct uni_struct *
add_entry (entry, current_entry)
union union_tag *entry; /* new value */
struct uni_struct *current_entry;
{
:
:
:
}
where *current_entry is the pointer to the
newly created and added entry in the linked list!
The function that will perform the memory space
allocation for a new node is called _______.
Answer here:
That is correct!
You've got it on the 2nd try.
Good, now you understand the concept!
It is used for dynamic memory allocation!
It returns a pointer to the allocated space!
Please review Lesson 10 Topic 3: Cast and Changing Object's Data Types
FORGET3
The correct answer is malloc().
Inside the function we need an auxiliary
pointer variable of type struct uni_struct;, i.e.
struct uni_struct *next_union;
The statement that will perform the memory
allocation for the structure, and assign the
returned address to the structure pointer is:
a if ((prt = (struct uni_struct ) malloc (
sizeof (struct uni_struct *))) != NULL) {
b if ((prt = (struct uni_struct * ) malloc (
(struct uni_struct ))) != NULL) {
c if ((prt = (struct uni_struct * ) malloc (
sizeof (struct uni_struct ))) != NULL) {
Select one:
That is correct!
You've got it on the 2nd try.
Good, now you understand the concept!
Think about the size and the type that are being allocated!
Try one more time!
Please review Lesson 10 Topic 3: Cast and Changing Object's Data Types
FORGET7
The correct choice is c ! The first choice is wrong
because the cast is the wrong type; the second choice is
wrong because the argument to malloc() is not an int.
The connection of the new node and copying of the
structure information will be done by:
ptr -> next_union = current_entry; /* connect */
ptr -> uni_var = *entry; /* copy value */
Finally, the function has to return the address of
the newly added node. And, if all pieces are put together
the internal function code can be represented as above:
{
char *malloc();
struct uni_struct *ptr; /* pointer to the
next list element */
if ((ptr = (struct uni_struct *) malloc (
sizeof(struct uni_struct))) != NULL) {
ptr -> next_union = current_entry;
ptr -> uni_var = *entry; /* copy value */
}
return ptr;
}