#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Ordered LinkedList add in middle


    While I labeled this as homework, I already turned in the assignment; however, I cannot sit easily until I understand what the hell I am doing wrong. What I have done here is implement an Ordered Linked List and an Ordered List Node. The add method() appends the items at the front and end of the list properly; however, when I attempt to compare the items, to sort them in order, I run into problems.

    public boolean add(Comparable obj) :

    Code:
    public boolean add(Comparable obj)
    	{
    		modCount++;
    		OrderedListNode newNode = new OrderedListNode(obj, null, null);
    		if( head == null) {
    		head = new OrderedListNode(obj, null, null);
    		return true;
    		
    		}
    			if(((Comparable)(head.theItem)).compareTo(obj) > 0) {
    		/////////////////This is the part that held me up, still not right though/////
    				new OrderedListNode(obj, null, null);//////
    				tail.next = newNode;
    				newNode = tail.previous;	
    				tail.previous.previous = newNode;
    				modCount++;
    				return true;//////////////////////////////////////////////////////////
    		//////////////////////////////////////////////////////////////////////////////
    			}else {
    		
    		tail.previous.next = newNode;
    		newNode.previous = tail.previous;
    		newNode.next = tail;
    		tail.previous = newNode;
    		return true;
    	}
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    My OrderedListNode class:

    Code:
    private static class OrderedListNode<Comparable> {
        	
    		Comparable theItem;
        	OrderedListNode<Comparable> next;
        	OrderedListNode<Comparable> previous;
        	
        	OrderedListNode(Comparable theItem) {
        		this(theItem, null, null);
        	}
        	
        	public OrderedListNode(Comparable theItem, OrderedListNode<Comparable> previous, OrderedListNode<Comparable> next) {
        		this.theItem = theItem;
        		this.next = next;
        		this.previous = previous;
        	}
        	
        	Comparable getData() {
                return theItem;
            }
    
            OrderedListNode getNext() {
                return next;
            }
    
            OrderedListNode getPrev() {
                return previous;
            }
        	
        }
        
      }

IMN logo majestic logo threadwatch logo seochat tools logo