Results 1 to 9 of 9
  1. #1
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default 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:

    Java Code:
    myLinkedListA<T>.addToStart(newData);
    addToStart looks like this:

    Java Code:
    public void addToStart(T itemData)
        {
            Node<T> newHead = new Node<T>(itemData, null, head);
    		  if (head != null)
    		  {
    		  	  head.previous = newHead;
    		  }
    		  head = newHead;
        }
    Could someone please let me know if they see something wrong?

    Thanks

  2. #2
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    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;
    }

  3. #3
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Thanks for the suggestions! BTW this a double linked list...

    Here is the code for the Node class:

    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;
            }
         }
    Thanks!

  4. #4
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    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.

  5. #5
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    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!

  6. #6
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    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;
    }

    }

  7. #7
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

  8. #8
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Thanks, It won't let me give you more 'rep' points because I've already given you too much. :)

  9. #9
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    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!

    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
    Here is the class that contains the code where the error is happening:

    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

  1. Replies: 4
    Last Post: 02-21-2011, 10:34 AM
  2. Need help using linked list
    By tigertomas in forum New To Java
    Replies: 5
    Last Post: 02-12-2011, 04:22 PM
  3. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-27-2010, 12:15 AM
  4. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 08:21 AM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •