Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Linked List add At Back

 
Kalabaw moo
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21412
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kalabaw moo
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21412
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21412
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! I didn't read it properly, did I. Multiple declarations on one line.

Sorry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic