TK1924 - Tutorial 04 Answer - Linked List
TK1924 - Tutorial 04 Answer - Linked List
1. Given the following declaration of node and illustration of a linked list with the pointers P1 and P2.
struct nodeType
{
int info;
nodeType *link;
};
list
• 18 32 23 16 43 87 25
P1 P2
c. Mark each of the following statements as valid or invalid. If a statement is invalid, explain why.
ii. list point to the node containing info 16. answer: list = P1->link->link;
v. Set the value of the node containing 25 to 35. answer: P1->link->link = 35;
vi. Create and insert the node with info 10 after the node pointed to by P1.
vii. Delete the node with info 23. Also, reallocate the memory occupied by this node.
e. Draw an illustration of the linked list after the execution of each of the statement below (* Use the original
illustration for each question)
i. P1 = P2link;
P2 info = P1info;
TK1924 Tutorial04 answer: Linked List 3
answer:
list
• 18 32 23 16 43 25 25
P2 P1
answer:
list
• 18 32 23 16 43 87 25
P1
P2
12
P3
iii. P4 = new nodeType;
P4info= P1info + 10;
P4link = P2link;
P2 link = P4;
answer:
list
• 18 32 23 16 43 87 25
P1
P2 42
P4
TK1924 Tutorial04 answer: Linked List 4
p = list;
while (p != NULL)
cout << p->info << “ “;
p = p->link;
cout << endl;
answer: 10 18 13
answer: 30 42 20 28