List Problem Set


  1. Given the following picture of a linked list, where head, P and Q are of type LLNode<String>:

    Write the code that will manipulate the linked list as follows. Each question should use the original list shown above.

    1. Make P point to the node containing the String "purple".
    2. Make Q point to the node containing the String "green".
    3. Make P point to the node containing the String "red".
    4. Make the node containing the String "green" point to the node containing the String "red".
    5. Make the node containing the String "purple" point to the node containing the String "green".
    6. Make the node containing the String "red" point to the node containing the String "purple".
    7. Concatenate the element field in the node P points to and the element field in the node Q points to and store the result in the node that currently contains "purple".
    8. Remove the node containing "blue" from the list.
    9. Remove the node containing "green" from the list.

  2. Given the following linked list, where P, Q and S are of type LLNode<String>:

    Show how each of the following statements changes the linked list. Draw a similar diagram and show the resulting list. Each question should be answered starting with the original list shown above.

    1. S.setInfo(P.getInfo());
    2. P.setInfo(head.getInfo() + Q.getInfo());
    3. S = Q.getLink();
    4. P.setLink(head);
    5. head.setLink(Q.getLink());
    6. Q = S.getLink();
    7. Q = Q.getLink();
    8. P.setLink(P);

  3. Write a function called traverse which has one parm of type LLNode<T>, which points to the first node in a list. Your function should print out the contents of each node in the list. If the list is empty, don't print anything.

  4. Write a function called delone which has one parm of type LLNode<T>, which points to the first node in a list. Your function should delete the first node in the list. If the list is empty, don't change anything.

  5. Write a function called delend which has one parm of type LLNode<T>, which points to the first node in a list. Your function should delete the last node in the list. If the list is empty, don't change anything.

  6. Write a function called search which has two parms. One is of type LLNode<T>, which points to the first node in a list. The second parm is of type T. Your function should search the list for the second parm, and return true if it is in the list and return false otherwise.

  7. Suppose p, q, and r are declared as type LLNode<String>. Show what is printed for each of the following, or explain why an error occurs:
    a.   p = new LLNode<String>(" ");
         p.setInfo("10");
         q = new LLNode<String>(" ");
         q.setInfo("15");
         q.setLink(p);
         p = new LLNode<String>(" ");
         p.setInfo(q.getInfo());
         System.out.println(p.getInfo() + " " + q.getInfo()
                                 + " " + q.getLink().getInfo());
    
    
    
    
    
    b.   p = new LLNode<String>("yes");
         p.setLink(new LLNode<String>("no"));
         q = p.getLink();
         q.setLink(null);
         p.setInfo("5");
         q.setInfo("10");
         r = new LLNode<String>("maybe");
         q.setLink(r);
         q.setInfo(r.getInfo());
         r.setInfo(p.getInfo());
         System.out.println(r.getInfo() + " " + q.getInfo()
                                   + " " + p.getInfo());
    
    


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2009