Results 1 to 5 of 5
- 05-11-2012, 06:14 PM #1
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:
Linked list class: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; } }
Testing it: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(); } ... } }
Result: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) {} }
So, the first index is removed, but the second one become null. Why is that?null
yo2
yo3
yo4
yo5
yo6
- 05-11-2012, 07:43 PM #2
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.
- 05-11-2012, 07:49 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Removing first element in a linked list do not work
No one here knows how your add and get methods looks like....
- 05-12-2012, 01:49 PM #4
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.
- 05-12-2012, 01:57 PM #5
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
-
Doubly Linked List removing help?
By Bungkai in forum New To JavaReplies: 0Last Post: 12-02-2011, 06:58 AM -
Linked List: Removing Largest Element Using Iterator
By helloworld12 in forum New To JavaReplies: 2Last Post: 05-08-2011, 05:28 AM -
How to access an element of a linked list inside another linked list?
By smtwtfs in forum New To JavaReplies: 4Last Post: 02-21-2011, 09:34 AM -
need help with removing repetitions from linked list...
By OptimusPrime in forum New To JavaReplies: 8Last Post: 03-09-2010, 08:29 PM -
Convert Linked List Object element to String
By CirKuT in forum New To JavaReplies: 2Last Post: 12-13-2008, 05:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks