LinkedList Problems!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CaseySimplified
    New Member
    • Feb 2008
    • 1

    LinkedList Problems!

    I am writing a LinkedList class from scratch without using the already defined LinkedList class. The only thing that doesn't seem to be working is adding removing and getting the last link in the list. It keeps returning null. Hope that helps! Thanks! Heres the code:

    Code:
    /**
    Casey Holgado
    Computer Science 4
    Period 2
    02/19/08
     */
    import java.util.*;
    public class LinkedList {
    
        public LinkedList()
        {
            first = null;
            last = null;
        }
        
        public Object getFirst()
        {
            if (first == null)
                throw new NoSuchElementException();
            return first.data;
        }
        
        public Object getLast()
        {
            if (last == null)
                throw new NoSuchElementException();
            return last.data;
        }
        
        public Object removeFirst()
        {
            if (first == null)
                throw new NoSuchElementException();
            Object element = first.data;
            first = first.next;
            currentSize--;
            return element;
        }
        
        public Object removeLast()
        {
            if (last == null)
                throw new NoSuchElementException();
            Object element = last.data;
            last = last.previous;
            currentSize--;
            return element;
        }
        
        public void addFirst(Object element)
        {
            Node newNode = new Node();
            newNode.data = element;
            newNode.next = first;
            first = newNode;
            currentSize++;
        }
        
        public void addLast(Object element)
        {
            Node newNode = new Node();
            newNode.data = element;
            newNode.previous = last; 
            last = newNode;
            currentSize++;
        }
        
        public int currentSize() 
        {
            return currentSize;
        }
        
        public ListIterator listIterator()
        {
            return new LinkedListIterator();
        }
        
        private Node last;
        private Node first;
        private int currentSize = 0;
        
        private class Node
        {
            public Object data;
            public Node next;
            public Node previous;
        }
        
        private class LinkedListIterator implements ListIterator
        {
            public LinkedListIterator()
            {
                position = null;
                previous = null;
                next = null;
            }
            
            public Object next()
            {
                if (!hasNext())
                    throw new NoSuchElementException();
                previous = position;
                
                if (position == null)
                    position = first;
                else
                    position = position.next;
                
                return position.data;
            }
            
            public Object previous()
            {
                if (!hasBefore())
                    throw new NoSuchElementException();
                next = position;
                
                if (position == null)
                    position = last;
                else
                    position = position.previous;
                
                return position.data;
            }
            
            public boolean hasNext()
            {
                if (position == null)
                    return first != null;
                else
                    return position.next != null;
            }
            
            public boolean hasBefore()
            {
                if (position == null)
                    return last != null;
                else
                    return position.previous != null;
            }
            
            public void add(Object element)
            {
                if (position == null)
                {
                    addFirst(element);
                    position = first;
                    
                }
                else
                {
                    Node newNode = new Node();
                    newNode.data = element;
                    newNode.next = position.next;
                    position.next = newNode;
                    position = newNode;
                    currentSize++;
                }
                previous = position;
            }
            
            public void remove()
            {
                if (previous == position)
                    throw new IllegalStateException();
                    
                if (position == first)
                {
                    removeFirst();
                }
                else
                {
                    previous.next = position.next;
                    currentSize--;
                }
                position = previous;
            }
            
            public void set(Object element)
            {
                if (position == null)
                    throw new NoSuchElementException();
                position.data = element;
            }
            
            private Node next;
            private Node position;
            private Node previous;
        }
        
            public static void main() 
        {
            LinkedList list = new LinkedList();
            ListIterator itr = list.listIterator();
            
            itr.add("second");
            itr.add("third");
            itr.add("fourth");
            list.addFirst("first");
            list.addLast("fifth");
            
            System.out.println("currentSize: " + list.currentSize());
            System.out.println("getFirst: " + list.getFirst());
            System.out.println("getLast: " + list.getLast());
            System.out.println("removeFirst: " + list.removeFirst());
            System.out.println("removeLast: " + list.removeLast());
            System.out.println("getCurrentSize: " + list.currentSize());
            System.out.println("getFirst: " + list.getFirst());
            System.out.println("getLast: " + list.getLast());
        }
    }





    Code:
    /**
    Casey Holgado
    Computer Science 4
    Period 2
    02/19/08
     */
    public interface ListIterator
    {
        Object next();
        Object previous();
        boolean hasNext();
        boolean hasBefore();
        void add(Object element);
        void remove();
        void set(Object element);
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    In addFirst, when you add the first element, first correctly points to that element. What does last point to? Since you never use addLast, last never changes, and when you call removeLast, it still points to its original value. You need a way to set last in addFirst, and first in addLast, if appropriate.

    Comment

    Working...