Name: ________________________________ Section: _________________

Honor Code Acknowledgment: ___________________


Random Quiz # 9 EXTRA CREDIT

CPS 6, Fall 1995

Due: December 6


Problem 1: Pointer Sisters (3 points)

What is output by the program below?

#include 

main()
{
    int num = 25;
    int * np = #
    *np = 32;
    int * list = new int[10];

    list[0] = 0;
    list[1] = num;
    np = &list[2];
    num = 13;
    *np = 55;

    cout << list[0] << " " << list[1] << " " << list[2] << endl;
}

Problem 2: Tail of Vienna Sausage (3 points)

Write the function LastNode whose header is given below. LastNode returns a pointer to the last node of list.

  struct Node
  {
      string info;
      Node * next;
  };


   Node * LastNode(Node * list)
   // postcondition: returns pointer to last node in list
   //                returns 0 if list is 0

Problem 3: How much is a Clone Fall? (3 points)

Write the function NumList whose header is given below. The call NumList(5) should return a pointer to the first node of a linked list whose nodes contain the values 1, 2, 3, 4, 5 in that order as diagrammed below.
     +-----+    +-----+    +-----+    +-----+    +-----+    
     |  1  |    |  2  |    |  3  |    |  4  |    |  5  |
     |    ----->|    ----->|    ----->|    ----->|    ----->NULL
     |     |    |     |    |     |    |     |    |     |
     +-----+    +-----+    +-----+    +-----+    +-----+    
      ^
      |
list--+

after the call list = NumList(5);



    Node * NumList(int num)
    // preconditoin: 0 < num
    // postcondition: returns pointer to linked list containing num values
    //                in order 1, 2, 3, ... , num