Linked List - insert between certain numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • July Lee
    New Member
    • Oct 2011
    • 1

    Linked List - insert between certain numbers

    I need to insert a new int into a list of its. BUT - It needs to be after a number smaller than it, and before a number larger than it, but the list is not to be sorted. It's driving me bonkers. I have 3 classes - all work right except for these assignments in my List class:
    if (current.getNex t() < temp && current > temp)
    while (current.getNex t() == index)

    I am very good at rookie errors - please help if you can...
    Code:
    public class List extends ListNode
    {
    	public ListNode head;
    	public int listCount;
    	
    	//constructor
    	public List()
    	{
    		head = null;
    		listCount=0;
    	}
    	
    	//to append the list
    	public void appendItem(int item)
    	{
    		ListNode newNode = new ListNode(item);
    		
    		if (head == null)		
    		{
    			head = newNode;
    		}
    		else					
    		{
    			// find last item in list
    			ListNode nextNode = head;
    			
    			while (nextNode.getNext() != null)
    			{
    				nextNode = nextNode.getNext();
    			}
    			
    			nextNode.setNext(newNode);
    		}
    	}
    	
    	
    	//to insert item at a certain point
    	public void insertItem(int item, int index)
    	{
    		ListNode temp = new ListNode(item);
    		ListNode current = head;
    		
    		while (current.getNext()!= null )
    			for(int i = 1; i < index && current.getNext() != null; i++)
    			{
    				if (current.getNext() < temp && current > temp)
    				{
    				current = current.getNext();
    				}
    			}
    		
    		temp.setNext(current.getNext());
    		current.setNext(temp);
    		listCount++;
    	}
    
    	// to delete a specific item
    	public boolean deleteItem(int index)
    	{
    		ListNode current = head;
    		if (head == null)
    		{
    			return false;
    		}
    		while (current.getNext() == index)
    		{
    			if(current.getNext() == null)
    				return false;
    			current = current.getNext();
    		}
    		current.setNext(current.getNext());
    		
    		return true;
    	}
    
    	// to print the list
    	public void printItem()
    	{ 
    		ListNode nextNode = head;
    		
    		while (nextNode!= null)
    		{
    			System.out.print(nextNode.getNext());
    		}
    	}
    	
    		
    	public ListNode getHead()
    	{
    		return head;
    	}
    
    }
    Code:
    public class ListNode 
    {
    	private int newItem;
    	private ListNode nextNode;
    	
    	
    	public ListNode()
    	{
    		newItem = 0;
    		nextNode = null;
    	}
    	
    	public ListNode(int item)
    	{
    		newItem = item;
    		nextNode = null;
    	}
    	
    	public void setNext(ListNode next)
    	{
    		nextNode = next;
    	}
    	
    	public ListNode getNext()
    	{
    		return nextNode;
    	}
    	
    	public void setItem(int item)
    	{
    		newItem = item;
    	}
    	
    	public int getItem()
    	{
    		return newItem;
    	}
    }
    Code:
    public class LinkedListDriver extends List 
    
    {
    
           public static void main(String[] args) 
    
           {
    
                  List list = new List();
    
           
    
                  list.appendItem(1);
    
                  list.appendItem(2);
    
                  list.appendItem(3);
    
                  list.appendItem(42);
    
                  list.appendItem(5);
    
                  list.appendItem(2);
    
                  
    
                  list.insertItem(0);
    
                  list.insertItem(5);
    
     
    
                  list.insertItem(4);
    
                  list.insertItem(192);
    
                  list.appendItem(9);
    
                  
    
                  list.insertItem(3);
    
                  list.appendItem(3);
    
                  
    
                  System.out.println("Print after insert and appends");
    
                  list.printItem();
    
                  
    
                  //list.deleteItem(9);
    
                  //list.deleteItem(1);
    
                               
    
                  //list.deleteItem(3);
    
     
    
                  System.out.println("Print after deletes");
    
                  list.printItem();
    
     
    
                  list.appendItem(44);
    
                  list.appendItem(22);
    
                  list.appendItem(1);
    
                  
    
                  System.out.println("Print prior to sort");
    
                  list.printItem();
    
                  
    
                  //list.sort();
    
                  
    
                  System.out.println("Print after sort");
    
                  list.printItem();
    
           }
    }
  • isprabu
    New Member
    • Oct 2011
    • 1

    #2
    Modified List class

    Your List class needs few modifications. I have made the changes in append, insert, delete and print methods. Now the list handles all those basic operations properly.

    Your other point "BUT - It needs to be after a number smaller than it, and before a number larger than it" is not clear to me. If you explain it in detail (with example), i can try to help you in that regard too.
    Hope this helps!

    Code:
    public class List extends ListNode {
    	    public ListNode head;
    	    public int listCount;
    	 
    	    //constructor
    	    public List()
    	    {
    	        head = null;
    	        listCount=0;
    	    }
    	 
    	    //to append the list
    	    public void appendItem(int item)
    	    {
    	        ListNode newNode = new ListNode(item);
    	 
    	        if (head == null)        
    	        {
    	            head = newNode;
    	        }
    	        else                    
    	        {
    	            // find last item in list
    	            ListNode nextNode = head;
    	 
    	            while (nextNode.getNext() != null)
    	            {
    	                nextNode = nextNode.getNext();
    	            }
    	 
    	            nextNode.setNext(newNode);
    	        }
    	        listCount++; // this is required!
    	    }
    	 
    	 
    	    //to insert item at a certain point
    	    public void insertItem(int item, int index)
    	    {
    	        ListNode temp = new ListNode(item);
    	        ListNode current = head;
    	
    	        if (index < 0 || index > listCount) return; // error
    	        if (index == 0) {
    	        	// replace 'head' position
    	        	temp.setNext(head);
    	        	head = temp;	        	
    	        } else {
    	            for(int i = 1; i < index && current.getNext() != null; i++)
    	            {
    	                //if (current.getNext().getItem() < temp.getItem() && current.getItem() > temp.getItem())
    	                //{
    	                //current = current.getNext();
    	                //}
    	                current = current.getNext();
    	            }
    		 
    		        temp.setNext(current.getNext());
    		        current.setNext(temp);
    	        }
    	        listCount++;
    	    }
    	 
    	    // to delete a specific item
    	    public boolean deleteItem(int index)
    	    {
    	        ListNode current = head;
    	        ListNode prev = null;
    	        int i = 0; 
    	        while (current != null) 
    	        {
    	        	if (i == index) { 
    	        		// matching location found
    	        		if (prev != null) {
    	        			prev.setNext(current.getNext());
    	        		} else {
    	        			// this means we are deleting the 'head' item
    	        			head = current.getNext();
    	        		}
    	        		listCount--;
    	        		return true;
    	        	}
    	        	prev = current;
    	        	current = current.getNext();
    	        	i++;
    	        }
    	        return false;
    	    }
    	 
    	    // to print the list
    	    public void printItem()
    	    { 
    	        ListNode nextNode = head;
    	 
    	        while (nextNode!= null)
    	        {
    	        	System.out.println(nextNode.getItem()); 
    	            nextNode = nextNode.getNext(); 
    	        }
    	    }
    }

    Comment

    Working...