MACS 262 Linked List Exercise
DUE: Monday, March 1, in class
Provide a hand trace of each of the following sections of code.
Assume that c, p, n and cursor are all node*.
#1. Assume you have the following list:
head->5->12->16
Draw a picture of the list after the following code is executed:
c = head;
while (c->link() != NULL)
{
c = c->link();
}
c->set_link(head);
while (head->link() != c)
head = head->link();
head->set_link(NULL);
head = c;
#2. Assume you have the following list:
head->100->200->300
Draw a picture of the list after the following code is executed:
c = head;
while (c->link() != NULL)
c = c->link();
c->set_link(head);
head = head->link();
c->link()->set_link(NULL);
#3. Assume you have the following list:
head->50->150->200->250
Draw a picture of the list after the following code is executed:
c = head;
n = NULL;
node* p = NULL;
while (c != NULL)
{
n = c->link();
c->set_link(p);
p = c;
c = n;
}
head = p;
#4. Assume you have the following list:
head->5->10->15->20->25->30
Draw a picture of the list after the following code is executed:
node* save;
n = head;
c = head;
if (head->link() != NULL)
{
save = head->link();
while (n->link() != NULL
&& c->link() != NULL)
{
n =
c->link();
if
(n->link() != NULL)
{
c->set_link(n->link());
c = c->link();
n->set_link(c->link());
}
}
c->set_link(save);
}
NOTE: It is OK to copy this code into a program and run it to test your
answer. But you should do the trace first. I will expect to
see a detailed trace on what you turn in.