CPSC 220 Fall 2002
Homework 1

  1. Use pointer and box notation to draw the list created by the following code. Assume that the Node class is visible and has the usual definition, with the val field holding an Object and the next field holding a reference to another Node.
      Node head = null;
      for (int i=0; i<5; i++)
        head = new Node(i+"", head);
    
      Node temp = head;
      while (temp.val < "3")
        temp = temp.next;
    
      temp.next = new Node("new", temp.next);
    

  2. In line 3 of the code above, why is the first argument to the Node constructor i+"" instead of just i?

  3. Assume that variable head points to the beginning of a singly linked list, and that the nodes hold Objects. Write code that copies the elements of the list into an array by doing the following:
    1. Count the number of nodes in the list.
    2. Create an array of the appropriate size.
    3. Copy the values from the linked list into the array.

  4. Assume that variable head points to the beginning of a singly linked list. Write code to remove the last item from the list and put it on the front of the list.