Chapter 9
# 15, 16, 17, 22 pages 495 to 497 
15. Assume that p, q, r, and nextNode are pointers to character nodes.  Initially, the nodes for p and q are dynamically allocated with values X and A, respectively.  The three upcoming statements build a portion of linked list.  After each statement, describe the resulting chain of nodes.
(a) r = new node<char> ('M');
(b) q->next = r;
  r->next = p;
(c) nextNode = q->next;
  q->next = nextNode->next;
  delete nextNode;
16. Use the following linked list of integer nodes for each part:

        i.e.
50--> 25--> 10--> 45--> 80 

Assume the declaration

node<int> *front, *p, *newNode, *nextNode;
(a) Consider the following statement, which addes a new node to the list:
front = new node<int> (30, front);
The value of the first node in the list is ________
The value of the second node in the list is ________
The value of the third node in the list is ________

(b) Assume that p points at the node with value 10. Execute the following statemrnts:

nextNode = p->next;
p->next = nextNode->next;
delete nextNode;
The integer value of node p after the statements is ______________
The value of the node that is deleted from the list is ______________

(c) The following loop scans the list:

p = front;
while (p != NULL)
   p = p-> next;
cout << p->next;
Will the cout statement execute properly?

(d) Modify the code in part (c) so that it outputs the value 80.
(e) Trace the following loop:

int t;
node<int> *q;

p = front;
while (p->next != NULL)
{
    q = p->next;
    t = p->nodeValue + q->nodeValue;
    p->nodeValue = t;
    p = q;
}

What are the data values for each node in the resulting list?

17. Apply the following function to the lists in parts (a) and (b), and display the new list:

template <typename T>
void f(node<T> * & front)
{
   node<T> *curr, *prev;

   if( front == NULL)
      return;

   prev = NULL;
   curr = front;

   while (curr->next != NULL)
   {
      prev = curr;
      curr = curr->next;
   }
   if (prev == NULL)
       front = NULL;
   else
       prev->next = NULL;

   delete curr;
}

(a) Assume that front points at the integer list
front -> 2 -> 8 -> 5 -> 7 -> 2 -> 9 ->25
(b) Assume that front points at the character list containing the elements "template."

22. Fill in the statements to build the following doubly linked list:

dnode<int> *header = ________, *curr, *newNode;

newNode = ________________________;
newNode->next = __________________;
header->prev = ___________________;
newNode->prev = __________________;
header->next = ___________________;
curr = ___________________________;
newNode = ________________________;
newNode->prev = __________________;
header->prev->next = _____________;
newNode->next = __________________;
header->prev = ___________________;