Vous êtes sur la page 1sur 5

TK1924 Tutorial04 answer: Linked List 1

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;
};

nodeType *P1, *P2, list;

The initial value of the list is illustrated as follows:

list

• 18 32 23 16 43 87 25

P1 P2

a. What is the output of each of the following C++ statements?

i. cout << list->info; answer: 18

ii. cout << list->link->info; answer: 32

iii. cout << p1->info; answer: 32

iv. cout << p2->link->info; answer: 23

v. cout << list->link->link->info; answer: 23

b. What is the value of each of the following relational expressions?

i. list->info >= 18 answer: true

ii. list->link == P1 answer: true

iii. P1->link->info == 16 answer: false

iv. P2->link == NULL answer: false

v. P2->link->info == NULL answer: false

c. Mark each of the following statements as valid or invalid. If a statement is invalid, explain why.

i. P1 = P2; answer: valid

ii. list->link = P1->link; answer: valid


TK1924 Tutorial04 answer: Linked List 2

iii. list->link->info = 45; answer: valid

iv. *list = P2; answer: valid

v. *P1 = *P2; answer: valid

vi. P2 = P1->link->info; answer: invalid

vii. P1->info = P2->info; answer: valid

viii. list = P2->link->link; answer: valid

ix. P2 = P2->link->link->link; answer: valid

d. Write C++ statements to do the following.

i. P1 point to the node containing info 23. answer: P1 = P1->link;

ii. list point to the node containing info 16. answer: list = P1->link->link;

iii. P2 point to the last node in the list. answer: P2 = P2->link;

iv. list point to NULL. answer: list = NULL;

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.

answer: nodeType * ptr;


ptr = new nodeType;
ptr ->info = 10;
ptr = P1->link;
P1 = ptr;
delete ptr;

vii. Delete the node with info 23. Also, reallocate the memory occupied by this node.

answer: nodeType *ptr


ptr = P1->link;
P1->link = ptr->link;
delete ptr;

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 = P2link;
P2 info = P1info;
TK1924 Tutorial04 answer: Linked List 3

answer:

list

• 18 32 23 16 43 25 25

P2 P1

ii. P3 = new nodeType;


P3 info = 12;
P3 link = NULL;
P1 link = P3link;

answer:

list

• 18 32 23 16 43 87 25

P1
P2
12

P3
iii. P4 = new nodeType;
P4info= P1info + 10;
P4link = P2link;
P2 link = P4;

answer:

list

• 18 32 23 16 43 87 25

P1
P2 42

P4
TK1924 Tutorial04 answer: Linked List 4

f. What is the output of the following C++ code?

p = list;
while (p != NULL)
cout << p->info << “ “;
p = p->link;
cout << endl;

answer: (18 infinite)

2. What is the output of the following C++ code.

a. list = new nodeType;


list->info = 10;
ptr = new nodeType;
ptr->info = 13;
ptr->link = NULL;
list->link = ptr;
ptr = new nodeType;
ptr->info = 18;
ptr->link = list->link;
list->link = ptr;
cout << list->info << “ “ << ptr->info << “ “;
ptr = ptr->link;
cout << ptr->info << endl;

answer: 10 18 13

b. list = new nodeType;


list->info = 20;
ptr = new nodeType;
ptr->info = 28;
ptr->link = NULL;
list->link =ptr;
ptr = new nodeType;
ptr->info = 30;
ptr->link = list;
list = ptr;
ptr = new nodeType;
ptr->info = 42;
ptr->link = list->link;
list->link = ptr;
ptr = list;
while (ptr != NULL)
{
cout << ptr->info << endl;
ptr = ptr->link;
}
TK1924 Tutorial04 answer: Linked List 5

answer: 30 42 20 28

Vous aimerez peut-être aussi