aspose file tools*
The moose likes Beginning Java and the fly likes Linked List add At Back Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Linked List add At Back" Watch "Linked List add At Back" New topic
Author

Linked List add At Back

Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Hi, I know that there is a package for linked list but I wanted to create my own linked list, the problem I'm having is I'm able to add a node in front of the list but if I add at the back it's still adding in front of the list. Here's the summarized code I created.



Hope you could give some advice.. thanks

edit: One thing I noticed is the list is always empty, so it's always adding in front. I don't have any idea, I've been sitting here for hours figuring out what to do.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Don't go straight to the keyboard. Get a pencil and paper and draw a linked list, with little boxes for all the nodes, and round blobs for the values in the nodes and arrows with head and tail, rather like this >>----->> for the links from one node to the next. Then put your finger on the paper and go through the pathway your data follow when you add something at one end of the list, or the other end.
Try drawing a doubly-linked list; the nodes are connected by >>----->> and <<-----<< and you have a direct connection to the last node. See whether you can think how to add at each end with such a list.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I can't understand the line about
(37) Node currentNode = list, newNode;
Does that actually compile? Please look at this FAQ.
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
As I've said, I can't figure out what's happening and been sitting for hours writing and thinking how to fix it.
I'm creating a singly linked list.

I'm using the:



to loop through the list till it reached the next null reference to add a node at the back of the list.
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Campbell Ritchie wrote:I can't understand the line about
(37) Node currentNode = list, newNode;
Does that actually compile? Please look at this FAQ.


I don't exactly understand what you mean with the FAQ you provided. But I'm 100% that this is my own code.
This is the class Node I created



edit: I just started coding the adding of item, I was able to do it in front of the node but not at the back. So as of right now that's the only code I have adding at back, because that's the only thing I'm having problem with.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Your printList() method changes the list instance variable. This means that that the method will work differently with each call. Is this what you want? Don't you want the method to print the whole list?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Henry Wong wrote:Your printList() method changes the list instance variable. This means that that the method will work differently with each call. Is this what you want? Don't you want the method to print the whole list?

Henry


Yes I wanted to print the list all at once and I wanted to display the list each time I'm adding an item in the list. I don't get the printList() changes the list instance variables?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Kalabaw moo wrote:
Henry Wong wrote:Your printList() method changes the list instance variable. This means that that the method will work differently with each call. Is this what you want? Don't you want the method to print the whole list?

Henry


Yes I wanted to print the list all at once and I wanted to display the list each time I'm adding an item in the list. I don't get the printList() changes the list instance variables?


This line (of the printList() method)....



When your printList() method completes, the list instance variable will point to the end of the list. The next time the printList() method is called, it will start from the end of the list.

Henry
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Henry Wong wrote:
Kalabaw moo wrote:
Henry Wong wrote:Your printList() method changes the list instance variable. This means that that the method will work differently with each call. Is this what you want? Don't you want the method to print the whole list?

Henry


Yes I wanted to print the list all at once and I wanted to display the list each time I'm adding an item in the list. I don't get the printList() changes the list instance variables?


This line (of the printList() method)....



When your printList() method completes, the list instance variable will point to the end of the list. The next time the printList() method is called, it will start from the end of the list.

Henry


I solved the issue now, I just passed an argument to the printList( Node list ) method and all things are properly working now. Thanks ( Wong ) for giving a hint..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Kalabaw moo wrote:I don't exactly understand what you mean with the FAQ you provided. But I'm 100% that this is my own code.
Line 37 looks like something which won't compile. So I was wondering whether it was the code you are actually running.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Campbell Ritchie wrote:
Kalabaw moo wrote:I don't exactly understand what you mean with the FAQ you provided. But I'm 100% that this is my own code.
Line 37 looks like something which won't compile. So I was wondering whether it was the code you are actually running.


Line 37 looks okay. Why won't it compile?

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Damn! I didn't read it properly, did I. Multiple declarations on one line.

Sorry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Linked List add At Back