List Problem Set Answers


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










  1.    public void traverse(LLNode<T> first) {
          LLNode<T> curr;
          for (curr = first ; curr != null ; curr = curr.getLink())
                System.out.println(curr.getInfo());
       }
    
  2.    public int delone(LLNode<T> first) {
          if (first != null)
              first = first.getLink();
          }
       }
    
  3.    public void delEnd(LLNode<T> first) {
          LLNode<T> curr = first;
          if (first == null)
             return;
          if (first.getLink() == null) {
             first = null;
             return;
          }
          while (curr.getLink().getLink() != null)
             curr = curr.getLink();
          curr.setLink(null);
       }
    
  4.    // we can change first without destroying the list because
       // first is passed by value, so even though we change it inside
       // the function, the original value will be unchanged
       boolean search(LLNode<T> first, T item) {
          if (first == null)
             return false;
          while (first != null) {
             if (first.getInfo().equals(item))
                return true;
             first = first.getLink();
          }
          return false;
       }
    
    1. 15    15    10
      
    2. 5    maybe    5
      


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

    © Copyright Emmi Schatz 2009