Results 1 to 10 of 10
Thread: LinearLinkedList
- 06-17-2010, 09:47 PM #1
LinearLinkedList
I recently started reading about Linked Lists and I have a couple of questions. Before I start asking, let me post the ListNode class:
Pretty straightforward. Next, there is the LinearLinkedList class:Java Code:package linkedLists; public class ListNode { private Object value; private ListNode next; public ListNode(Object value, ListNode next) { this.value = value; this.next = next; } public Object getValue() { return value; } public ListNode getNext() { return next; } public void setValue(Object value) { this.value = value; } public void setNext(ListNode next) { this.next = next; } }
Question 1: addFirst()Java Code:package linkedLists; public class LinearLinkedList { ListNode firstNode; public LinearLinkedList() { firstNode = null; } public boolean isEmpty() { return firstNode == null; } public ListNode getFirstNode() { return firstNode; } public void setFirstNode(ListNode node) { firstNode = node; } public void addFirst(Object o) { if (isEmpty()) firstNode = new ListNode(o, null); else { //[I]implementation shown below[/I] } } public void addLast(Object o) { if (isEmpty()) firstNode = new ListNode (o, null); else { //[I]implementation shown below[/I] } } [I]...other codes will be implemented later. (not in this thread)[/I] }
The book implements the missing code like this:
When I did it, I did another way:Java Code:firstNode = new ListNode(o, firstNode);
The question is, are those two similar?Java Code:ListNode first = new ListNode(o, null); first.setNext(firstNode.getNext()); firstNode = first;
Question 2: addLast()
The book implements the missing code like this:
Mine on the other hand, gets a null pointer access (yellow underline in eclipse. The underlined word is "current" in "current.setNext(new ListNode(o, null));"Java Code:ListNode current = firstNode; while (current.getNext() != null) current = current.getNext(); current.setNext(new ListNode(o, null));
Why am I getting the null pointer access? Also, why doesn't the book's code have the following:Java Code:while ((current = current.getNext()) != null); [U]current[/U].setNext(new ListNode(o, null)); firstNode = current;
Don't I need to reference firstNode to current because current is a local variable?Java Code:firstNode = current;
Thanks in advance!!Last edited by Lil_Aziz1; 06-18-2010 at 01:07 AM.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-17-2010, 10:37 PM #2
Its better to ALWAYS use {} with loops and if statements.
On What condition do you fall out of the while loop? What value does current have then?
- 06-17-2010, 11:06 PM #3
I fall out of the while loop when current.getNext() is equal to null.
EDIT: OHH. so the book's while loop is one iteration behind me, which is where it's suppose to be. Mine goes one iteration too far; thus, it's value is null!
Thanks! Rep+! What about the first question?"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-17-2010, 11:08 PM #4
vswhile (current.getNext() != null)
current = current.getNext();
Some day you'll add a line after the while and everything will go to s.while (current.getNext() != null) {
current = current.getNext();
}
- 06-18-2010, 12:27 AM #5
I don't know if you noticed, but there is a semicolon after the while loop:
I intentionally put it the semicolon so all it does is iterate through the firstNode and make current get to the last element.Java Code:while ((current = current.getNext()) != null); current.setNext(new ListNode(o, null)); firstNode = current;
I learned that the following two snippets are equivalent.
Java Code:while ((current = current.getNext()) != null);
Although I tried it with the {} and nothing changed. I'm still getting the null pointer access.Java Code:while ((current = current.getNext()) != null) { }Last edited by Lil_Aziz1; 06-18-2010 at 12:29 AM.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-18-2010, 12:51 AM #6
The first loop does NOT have a ; after the condition:
The book implements the missing code like this:
Code:
ListNode current = firstNode;
while (current.getNext() != null)
current = current.getNext();
current.setNext(new ListNode(o, null));
Mine on the other hand, gets a null pointer access (yellow underline in eclipse. The underlined word is "current" in "current.setNext(new ListNode(o, null));"
Code:
while ((current = current.getNext()) != null);
current.setNext(new ListNode(o, null));
firstNode = current;
- 06-18-2010, 01:06 AM #7
Right. But aren't the {} implied if there is only one line of code to execute? I double checked the book and this is exactly how they had it so it's not a typo on my part.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-18-2010, 02:14 AM #8
I'm not talking about legal statements.
I'm talking about safe code.
- 06-18-2010, 03:25 AM #9
Oh okay.
Going back on topic, I think I got almost all my questions answered except this part:
Don't I need to reference firstNode to current because current is a local variable?Java Code:firstNode = current;
Why don't we have to do that?"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-18-2010, 08:05 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
With code like that you're taking one step too many and you're falling of your list. The last element of your list is the element which doesn't have a next element (read that again ;-) so you have to change your loop like this:
The test current != null is necessary in case the list is empty. While the current element has a next element it 'hops' to the next element, otherwise the loop terminates and current points to the last element in the list.Java Code:for (current= firstElemt; current != null && current.getNext() != null; current= current.getNext());
kind regards,
Jos


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks