Results 1 to 9 of 9
Thread: help with linked list
- 03-25-2011, 06:33 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
help with linked list
In my program I use a linked list with an iterator.
I have a compile time error that I can't solve.
The error is:
illegal start of expression
for this line:
addToStart looks like this:Java Code:myLinkedListA<T>.addToStart(newData);
Could someone please let me know if they see something wrong?Java Code:public void addToStart(T itemData) { Node<T> newHead = new Node<T>(itemData, null, head); if (head != null) { head.previous = newHead; } head = newHead; }
Thanks
- 03-25-2011, 06:55 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
will you please summit your Node class .
each node is contains two fields one for link and another one for info or data
so the constructor parameter should contain (eaither next or Data / or both) for single linked list.
but i see in your const. you have head and null
Node<T> newHead = new Node<T>(itemData, null, head);
try to change your constructor in the node class and then change this one to
Node<T> newHead = new Node<T>(itemData);
example :
Dnode newLink = new Dnode(d);
if (isEmpty())
last = newLink;
else
first.setBack(newLink);
newLink.setNext(first);
first = newLink;
}
- 03-25-2011, 07:17 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
Thanks for the suggestions! BTW this a double linked list...
Here is the code for the Node class:
Thanks!Java Code:private class Node { private String item; private Node link; public Node( ) { item = null; link = null; } public Node(String newItem, Node linkValue) { item = newItem; link = linkValue; } }
- 03-26-2011, 04:48 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
in DLL we need assign back node to be able to us move backward. so as i said look to your const.
public Node(String newItem, Node linkValue)
and you can make it just like is but that does not matter now :
public Node(String newItem)
and look when you declared a new node in myLinKedList class:
Node<T> newHead = new Node<T>(itemData, null, head);
there is three values in parameters .. and that is wrong !
you have make your parameter have only two elements (String data , Link node "Whateve you called them")
.
and you need getter and setter in Node class .. because your fields are private.
- 03-26-2011, 01:49 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
I'm sorry, I pasted in the wrong code...
here is the correct code to my doubly linked list:
Java Code:private class Node<T> { private T data; //private Node<T> link; //doubly linked private Node<T> next; private Node<T> prev; public Node( ) { data = null; next = null; prev = null; } public Node(T newData, Node<T> nextNode, Node<T> prevNode) { data = newData; next = nextNode; prev = prevNode; //link = linkValue; } }//End of Node<T> inner class
Sorry for the confusion!
- 03-26-2011, 02:40 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
Look because fields are private you should have getter and setter to access them !
look to this example :
---------------------------
Java Code:public class Dnode { [COLOR="SandyBrown"]// FIELDS [/COLOR] [COLOR="DeepSkyBlue"]private[/COLOR] Object Data; [COLOR="DeepSkyBlue"] private[/COLOR] Dnode next; [COLOR="DeepSkyBlue"]private[/COLOR] Dnode back; [COLOR="SandyBrown"]// COSTR.[/COLOR] public Dnode(Object Data) { this.Data = Data; } [COLOR="SandyBrown"]// [B]Data[/B] getter[/COLOR] public Object getData() { return Data; } [COLOR="SandyBrown"]// [B]back[/B] getter[/COLOR] public Dnode getBack() { return back; } [COLOR="SandyBrown"]// [B]prev[/B] getter[/COLOR] public Dnode getNext() { return next; } [COLOR="SandyBrown"]// [B]Data[/B] setter[/COLOR] public void setData(Object Data) { this.Data = Data; } [COLOR="SandyBrown"]// [B]Back[/B] setter[/COLOR] public void setBack(Dnode back) { this.back = back; } [COLOR="SandyBrown"]// [B]next[/B] setter[/COLOR] public void setNext(Dnode next) { this.next = next; } }
second thing here in your linked list class .. I'll repeat that again you have to change it as you declared in Node class :
public void addToStart(T itemData)
{
Node<T> newHead = new Node<T>(itemData, null, head);
if (head != null)
{
head.previous = newHead;
}
head = newHead;
}
Let's test your code line by line to mention your error :
first it will create node first with information itemData alright .
and the your next node will be null , and previous one will be head !! is that possible??? !! .. to avoid that change your constructor in the Node class to :
public Node(T newData)
{
data = newData;
}
and that will not affect your code at all .
second :
let me explain to you several things :
first : head contain the address of the first node in the list ..
to avoid lost it .. we pointer new node let's called current and then keep move until null to stop .
REMEBER IF HEAD IS NULL THAT MEAN'S THERE IS NO LIST !! WE LOST IT IF WE KEEP "THE HEAD MOVING THROUGH THE LIST " .. so that we need to declare new node as a pointer.
ask your self if i said head.back what that mean !
this mean the back link in first node !
example
[head]--[1|01]=[2|02]=[3|03]=||
01 , 02 , 03 -- are the address of the next node
1,2,3 are the data.
the address in the head give us the data in the first node --> 1
head.next the address in the first node give as 2"the data in second node"-->2 and so one
Now Focus !! if we write head.back will give as the head it self !!
so now when you declare the the methods you have to know how they work you want add at first !
we have two cases :
1. if the list is empty ,
2. if not !
let's focus on second one :
you start correctly !
head.back=newNode;
list look what will happen
[head]--[1]-
so now you say to your program .. the node before first node is "newNode" .. is that enough !! of course
we still have link next , and we have to change the address of the head .
the last step you made it "Changing the address of the head"is correct but you forget important thing ..
so you have to say
newnode.next = head ;
here you will tell your program .. the node after the new node its address in the head so copy the address and paste it in the next link field in the new node. ( Draw the list before and then you will avoid the errors =) )
Now that what we have :
public void addToStart(T itemData)
{
Node<T> newHead = new Node<T>(itemData); // first change
if (head != null)
{
head.previous = newHead;
newHead.next = head;
head = newHead;
}
}
- 03-26-2011, 02:50 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
look at these files they will help you :
http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
http://www.iitk.ac.in/esc101/2009Jan.../lecture35.pdf
- 03-28-2011, 02:16 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
Thanks, It won't let me give you more 'rep' points because I've already given you too much. :)
- 03-28-2011, 04:31 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
Hi SHE,
Ok I made all the changes you suggested to make. I also read through the 2 pdf links you sent...thanks for those.
But I am still getting errors in my iterator class. I've tried various "fixes" but none seem to help.
I was wondering if you could take a quick look.
Thanks!
Here is the class that contains the code where the error is happening:Java Code:LinkedList3.java:104: illegal start of expression LinkedList3<T>.this.addToStart(newData); ^ LinkedList3.java:104: not a statement LinkedList3<T>.this.addToStart(newData); ^ LinkedList3.java:105: 'else' without 'if' else ^ 3 errors
Java Code:public class ListIterator { // We do not need a previous node when using a doubly linked list private Node<T> position = null; public ListIterator() { position = head; } public void restart() { position = head; } public String next() { if (!hasNext()) throw new IllegalStateException(); String toReturn = position.item; position = position.next; return toReturn; } public boolean hasNext() { return (position != null); } public String peek() { if (!hasNext()) throw new IllegalStateException(); return position.item; } public void insertHere(String newData) { if (position == null && head != null) { // add to end of list. First move a temp // pointer to the end of the list Node<T> temp = head; while (temp.next != null) { temp = temp.next; } temp.next = new Node<T>(newData); } else if (head == null || position.previous == null) // at head of list LinkedList3<T>.this.addToStart(newData); else { // Insert before the current position //Node<T> temp = new Node<T>(newData, position.previous, position); Node<T> temp = newNode<T>(newData); position.previous.next = temp; position.previous = temp; } } public void delete() { if (position == null) throw new IllegalStateException(); else if (position.previous == null) { // Deleting first node head = head.next; position = head; } else if (position.next == null) { // Deleting last node position.previous.next = null; position = null; } else { position.previous.next = position.next; position.next.previous = position.previous; position = position.next; } } } // listIterator
Similar Threads
-
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 using linked list
By tigertomas in forum New To JavaReplies: 5Last Post: 02-12-2011, 03:22 PM -
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
Linked list
By rosh72851 in forum New To JavaReplies: 1Last Post: 02-05-2009, 07:21 AM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks