Results 1 to 4 of 4
  1. #1
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default Linked List help

    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:


    Java 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() {} 
        }
        
    }

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    You want someone to do your homework, for free? ;)
    My Hobby Project: LegacyClone

  3. #3
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    I was just wondering if someone can help me on how to approach these methods.

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    For insertAfter get the Cell at n, if there is one. Set the new Cell's next to the found Cell's next. Set the found Cell's next to the new Cell.

    before:
    Cell(n) --> Cell(n+1)

    after:
    Cell(n) --> Cell(new) --> Cell(n+1)

    Similar idea for set
    My Hobby Project: LegacyClone

Similar Threads

  1. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 07:21 AM
  2. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM
  3. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 03:15 AM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 03:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •