Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [20pts] We have variables L and t, both of type Node*, with the values depicted in the figure below:
    
    struct Node
    {
      int data;
      Node* next;
    };
    

    On the picture:
    • Draw a rectangle around L.
    • Draw a circle around *L.
    • Mark X on L->data
    • Mark V on L->next.
    • Draw a triangle around L->next->data.
    • Add the result of running the following code:
      Node* p = L;
    • Add the result of running the following code:
      Node* q = L->next;
    • What is the type of L?

    • What is the type of *L?

    • What is the type of L->data?

  2. [10pts] Consider the above problem again.
    
    struct Node
    {
      int data;
      Node* next;
    };
    
    Write statement(s) that perform the following. Note: each problem is independent, meaning that changes in one part are not carried forward into the next part.
    1. Change the 8 to a 13:
      
      
      
      
      
    1. Change the 2 to a 6:
      
      
      
      
      
    1. Add to the front of the list L the node that t points to; i.e. after these statements, L should point to the list 5,8,2,7.

      Warning: Don't create any new node! There is a node with value 5 already in the picture -- use it.

      
      
      
      
      
      
      
      
  1. [10pts] Consider the following code:
    
    struct Node
    {
      string data;
      Node* next;
    };
    
    int main()
    {
      Node* temp = new Node;
      temp->data = "world";
      temp->next = NULL;
    
      Node* List = new Node;
      List->data = "Hello";
      List->next = temp;
    
      return 0;
    }
    
    Draw a picture that depicts the code (like the picture in problem 2):
  2. [60pts] Write a program that reads in ints from the user until a negative number is entered, storing them into a linked list. Then print out the 3rd-to-last number that was entered. (You can assume that at least 3 numbers will always be entered before a negative.)

    Hint: Store the numbers in your linked list in reverse order by calling add2front repeatedly (why will the list have reverse order?) as you read each number in.

    Example runs:

    ~/$ ./hw
    7 2 8 -1
    7
    
    Hint 2: Don't hesitate to draw a picture of some small examples by executing add2front() one by one.
    ~/$ ./hw
    1 2 3 4 5 6 7 8 9 -1
    7
    ~/$ ./hw
    4 8 2 7 4 9 -1
    7
    

Makefile

Use the following Makefile and fill appropriately so that make hw compiles your source code into a program called hw.

Turn in