Thursday, March 5, 2009

The following code should add a new element to the beginning of a linked list

The pointer head should point to the start of the list.
It is called as: Push(head, 192);

1: void Push(struct node* head, int data) {
2: struct node* newNode = malloc(sizeof(struct node));
3: newNode->data = data;
4: newNode->next = head;
5: head = newNode;
6: }

Given the scenario described above, what changes need to be made to the code in order for it to work as intended?
Choice a: Line 2 should be: struct node* newNode = malloc(sizeof(struct *node)); and change head in lines 4 and 5 to *head.
Choice b: Line 1 should be: void Push(struct node** head, int data) and change head in lines 4 and 5 to *head.
Choice c: The node struct needs to be created within the Push function prior to being used.
Choice d: Line 1 should be: void Push(struct node** head, int data).
Choice e: When calling the function, the value needs to be 287 or less due to the type specified in the function definition.

No comments: