aspose file tools*
The moose likes Beginning Java and the fly likes Issues with Linked List Add methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Issues with Linked List Add methods" Watch "Issues with Linked List Add methods" New topic
Author

Issues with Linked List Add methods

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Hi all. I created a Linked List and a Node class and I'm having some errors with my append and prepend method. For some reason, Instead of getting the output "dog, cat, bird" I'm getting "dog, dog, cat" and tail.next points to bird rather than null. When I start with an empty list and add cat, the head, tail, and tail.next nodes are correct, but once I try to prepend dog, everything gets messed up. Could someone please help me figure out whats wrong with these methods? Thanks!
SLL<T> class


Node<T>


Driver
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Hi Kd Martin,

Can you give the complete listing of class SLL?

Also, I think your prepend method is not proper. It is simply changing head to the new value, but what about the original list? Ideally, it should assign new head's tail to old head, and then assign new value to new head.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Kd Martin wrote:Could someone please help me figure out whats wrong with these methods?

As Anayonkar said, we really need to see the SLL code; however, one thing you might want to think about is adding an insert() method to your Node, viz:which inserts a value between the current Node and the one following it (or after it, if it's the tail), returning the inserted Node. This allows you to insert a value anywhere you like in the SLL; not just at the beginning or end.
Note that I didn't make it public, because it's only likely to be used by the SLL; but there's nothing to stop you doing so if you want.

Also, you can use it to implement your append() method, viz:I'd also suggest adding an addFirst(T data) method for adding the first Node to the list. It isn't absolutely required, but I think it'll make your code a bit clearer - the fact that the operation is very similar to a prepend() is an implementation detail.

Another thing: I see that your SLL has a size() method, but I don't see anywhere that you're updating it in your prepend/append methods. Now it may be that you're doing it somewhere else (again, code please), but they would seem to me to be the obvious place to do it.

Finally, you might consider making Node a static nested class, since it really only has any meaning to your SLL. It also allows you to make virtually everything, including its constructors, private, which increases security.

HIH

Winston



Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issues with Linked List Add methods
 
Similar Threads
How the insertAtBack sets the next for head?
Reversing a doubly linked list
Saving to a txt file
link list
what is the code trying to say between inheritance and aggregation?