Results 1 to 5 of 5
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Removing first element in a linked list do not work

    I have created my own linked list class for learning purpose. Everything works great except removing the first element. I cant really see why it wont work.

    The Node:
    Java Code:
    	private static class Node
    	{
    		Object obj;
    		Node nextNode;
    		
    		Node ()
    		{
    			obj = null;
    			nextNode = null;
    		}
    		
    		@SuppressWarnings("unused")
    		Node (Object obj, Node nextNode)
    		{
    			this.obj = obj;
    			this.nextNode = nextNode;
    		}
    		
    		void storeObject (Object obj)
    		{
    			this.obj = obj;
    		}
    		
    		void linkTo (Node nextNode)
    		{
    			this.nextNode = nextNode;
    		}
    		
    		Node getLink ()
    		{
    			return nextNode;
    		}
    		
    		Object getObject ()
    		{
    			return obj;
    		}
    	}
    Linked list class:
    Java Code:
    public class LinkedList<E>
    {
    	private Node first;
    	private Node last;
    	private int elements;
    	...
    
    	public void remove (int index)
    	{
    		if (index == 0)//Remove the first node
    		{
    			first = first.getLink();
    		}
    		...
    	}
    }
    Testing it:
    Java Code:
    	public static void main (String[] args)
    	{
    		LinkedList<StringBuilder> links = new LinkedList<StringBuilder> ();
    		links.add(new StringBuilder ("yo0"));
    		links.add(new StringBuilder ("yo1"));
    		links.add(new StringBuilder ("yo2"));
    		links.add(new StringBuilder ("yo3"));
    		links.add(new StringBuilder ("yo4"));
    		links.add(new StringBuilder ("yo5"));
    		links.add(new StringBuilder ("yo6"));
    		
    		links.remove(0);
    
    		try
    		{
    			for (int i = 0; i < 100; i++)
    				System.out.println(links.get(i));
    		}
    		catch (Exception e) {}
    		
    	}
    Result:
    null
    yo2
    yo3
    yo4
    yo5
    yo6
    So, the first index is removed, but the second one become null. Why is that?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Removing first element in a linked list do not work

    Try debugging the code by printing out the values of variables as the are changed and used. The print out should show you what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Removing first element in a linked list do not work

    No one here knows how your add and get methods looks like....

  4. #4
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Removing first element in a linked list do not work

    Norm: sorry, but I didnt quite understand where to put the debuggers.
    Anyway, here is my get and add methods:
    Java Code:
    	public void add (E object)
    	{
    		//If there is no elements(i e it is empty), this statement will run. The constructor initiate first to null and last to an empty node.
    		if (first == null)
    		{
    			Node node = new Node ();
    			first = node;
    			first.storeObject(object);
    			first.linkTo(last);
    		}
    		else
    		{
    			Node node = new Node ();
    			node.storeObject(object);
    			last.linkTo(node);
    			last = node;
    		}
    		elements++;
    	}
    
    	@SuppressWarnings("unchecked")
    	public E get (int index)
    	{
    		if (index > elements - 1 || index < 0)
    			throw new IndexOutOfBoundsException (index + "");
    		
    		if (index == 0)
    			return (E) first.getObject();
    		if (index == elements - 1)
    			return (E) last.getObject();
    		
    		Node node = first;
    		for (int i = 0; i <= index; i++)
    			node = node.getLink();
    		
    		return (E) node.getObject();
    	}
    Last edited by Pojahn_M; 05-12-2012 at 01:53 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Removing first element in a linked list do not work

    Add printlns after every line where the values of variables are changed and before where the contents of variables are used in decisions.
    For example at line 2 print out the value of first.
    At line 35 print the value of node.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Doubly Linked List removing help?
    By Bungkai in forum New To Java
    Replies: 0
    Last Post: 12-02-2011, 06:58 AM
  2. Linked List: Removing Largest Element Using Iterator
    By helloworld12 in forum New To Java
    Replies: 2
    Last Post: 05-08-2011, 05:28 AM
  3. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  4. need help with removing repetitions from linked list...
    By OptimusPrime in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 08:29 PM
  5. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 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
  •