Jump to content

Linked List help

- - - - -

  • Please log in to reply
No replies to this topic

#1
alpdog14

alpdog14

    Newbie

  • Members
  • Pip
  • 3 posts
Since this forum has been such a great help for me I thought I would come back for more help. So I have a LinkedList and am trying to create some methods to test in the J-Unit of Blue J, if anyone can help me create my:

set(int n, T value)
insertAfter (int n, T value)
LinkedList<T> reverse()

methods I would be most appreciated.

Here is my Code:


import java.util.Iterator;


public class LinkedList<T>

{

   

    static class Cell<T> {

       private T data;

       private Cell<T> next;

       Cell(T s, Cell<T> n) {data = s; next = n;}

       T data() {return this.data;}

       Cell<T> next() {return this.next;}

       void setData(T s) {data = s;}

       void setNext(Cell<T> c)   {next = c;}

    }


    private Cell<T> head;

    

    public LinkedList() {head = null;}

    

    public void addfirst(T data) {head = new Cell<T>(data, head);}

    

    public void printList() {

        Cell<T> p = head;

        while (p!=null) {

            System.out.println(p.data());

            p = p.next();

        }

    }

    

    public Iterator<T> iterator() {return new ListIterator<T>(this);}

    

    @SuppressWarnings("unchecked")

    public boolean equals (Object theOther) {

        if (! (theOther instanceof LinkedList)) return false;

        LinkedList<T> otherList = (LinkedList<T>) theOther;

        return equals(head,  otherList.head);

    }

    

    private boolean equals(Cell<T> p1, Cell<T> p2) {

        if (p1==null && p2==null) return true;             // both lists empty

        if (p1==null || p2==null) return false;            // one empty, one not

        if (! p1.data().equals(p2.data())) return false;   

        return equals(p1.next(), p2.next());                

    }

    

    public LinkedList<T> clone() {

        LinkedList<T> c = new LinkedList<T>();

        c.head = clonecells(head);

        return c;

    }

    

 

    private Cell<T> clonecells(Cell<T> c) {

        if (c==null) return null;

        return new Cell<T>(c.data(), clonecells(c.next()));

    }

    

    

    

    public int size() {

        int theSize;

        theSize = 0;

        return theSize;

    }

    

  

    public T get(int n) {

        Cell<T> p = head;

        while (n>size()) {

            p = p.next();

            n--;

            if(p==null){

                return null;

            }

        }

        return p.data();

    }

    


    public void set(int n, T value) {

      

        return;

    }

    

  

    public void insertAfter (int n, T value) { 


    }

    


    public LinkedList<T> reverse() {

        return null;

    }

    


    static public void demos()

    {

        Cell<String> L = null;

        L = new Cell<String>("cherry", L);

        L = new Cell<String>("blueberry", L);

        L = new Cell<String>("apricot", L);

        

        Cell<String> p;

        p = L;

        while (p != null) {

            System.out.println(p.data());

            p = p.next();

        }

        System.out.println("=========");

        

        p = L;

        p = p.next();

        Cell<String> q = new Cell<String>("grape", p.next());

        p.setNext(q);

        

        p = L;

        while (p != null) {

            System.out.println(p.data());

            p = p.next();

        }

       

        

    }

    

    private class ListIterator<T> implements Iterator<T>{

        

        private LinkedList<T> theList;

        private Cell<T> current;

        

        public ListIterator(LinkedList<T> LL) {

            theList = LL;

            current = theList.head;

        }

        

       

        public boolean hasNext() {

            return false; 

        }

        

        

        public T next() {

            return null; 

        }

        

        public void remove() {} 

    }

    

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users