BillHung.Net


powered by FreeFind     sms

Quiz 02

Question 1:
Define a struct for a node of a linked list of strings.

typedef struct nodetag{
char* string;
struct nodetag *next;
}LIST;

typedef struct nodetag{
char* string;
struct nodetag *next;
}LIST;
Question 2:
Suppose that the variable named "head" contains a pointer to the first node
of a linked list, defined as in #1, and the variable named "name" contains
a pointer to the first character of a C-style string.
Give a program segment that adds a node containing a copy of the string
to the head of the list.

The diagrams below show an example of the effect of your statement.

Before your statement:

name head
+---+ +---+ +-------+
| * | | *-|--->| * | *-|---> ...
+-|-+ +---+ +-|-----+
| |
v v
"bush" "clinton"

After your statement:

name head
+---+ +---+ +-------+ +-------+
| * | | *-|--->| * | *-|--->| * | *-|---> ...
+-|-+ +---+ +-|-----+ +-|-----+
| | |
v v v
"bush" "bush" "clinton"

newnode = (LIST *) malloc (sizeof(LIST));
newnode->next = head;
head = newnode;

sizeofStr = strlen (name);/*needs <string.h>*/

newnode->string = (char *) calloc( sizeofStr+1, sizeof(char) ); /*+1 is for the null character*/

strcpy(newnode->string, name); /*copy the string from name to string*/


Question 3:
What did you find difficult or confusing about the reading? If
nothing was difficult or confusing, tell us what you found most
interesting. In either case, please be as specific as possible.

There're nothing particularly difficult. I can't think of anything to say. Sorry.