• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

removing from a linked List

 
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI guys I'm trying to make my own linked List I am having some problems with my remove function I can't seem to delete the node with the data one

why isn't this node been removed? and any tips on how I could solve this problem?

thanks





 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried changing the code a bit but to my surprise it still does not delete the node I want

 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I can't seem to delete the node with the data one


When working with links in a list  I draw a diagram of a list with nodes showing how the links need to be changed when changing the contents of the list.
Have you done that?
Can you describe what links need to be changed when removing a node from the list?

Also the code may need to be debugged to detect where it is not doing what you expect.  How are you trying to debug the code?
I use print statements to show the values of the variables as they are changed and used.

Can you post the printed output from when the program is executed.
Print the list before and after any changes.
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Norm yes I am working on a piece of paper drawing the linked list it works perfect until when I try to remove the tail of the list I am going through it on the paper and it makes sense on paper what I'm doing,in theory it should work but it seems to not remove it


I'm guessing there is something wrong with this part of the code but I can't tell what





this is the output

ID IS 4 50 Edward
ID IS 3 40 Mark
ID IS 2 30 Bill
ID IS 1 20 Jeff
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 I can't tell what


Add some print statements so you can see what the computer sees when the code is executed.  

Another technique I find very useful is to design the code before trying to write it
and then add comments to the methods that describe in detail what the method is going to do and how it it going to do it.
For example for the remove method, what is it going to do for the 3 cases when the node to be removed is
at the head
in the middle
at the tail
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mainly what I am trying to do is change where the tail points to if the tail is picked to be deleted which is the Node with data 1 in the list

I'm still guessing it could be something wrong with this part of the code

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we cross posted. You seem to have missed what I have suggested.

Another technique for debugging lists is to add a toString() method to the Node class:

Then in the List class, printing the current node will show you the list.
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Norm thanks for the reply I added some println statements to debug





deleting tail node gets printed like what I expected to the node does not actually get deleted because it still prints the whole list with the node(data 1)

deleting tail Node
ID IS 4 50 Edward
ID IS 3 40 Mark
ID IS 2 30 Bill
ID IS 1 20 Jeff
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Chalkley wrote:



If this code block is for checking, and then deleting, the head node... then why is the code checking the node pointed to by the current reference?? Is the current reference always the same as the head node, for all iterations of the loop?

Henry
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'll leave it to you Henry.

Advice from two different ways will only confuse the OP
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



If this code block is for checking, and then deleting, the head node... then why is the code checking the node pointed to by the current reference?? Is the current reference always the same as the head node, for all iterations of the loop?



Hi Henry I'm not too sure what you mean but I changed the code you brought up but I'm still having problems deleting the tail node


 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyone know why the node isn't deleting I've gone through it on a piece of paper several times and in theory it should actually get removed but it doesn't.
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys I figured it out I forgot to modify the currents next node

here is the updated code




if anybody wants to input some constructive critism on how I could make the code better that would be greatly appreciated.

thanks
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Chalkley wrote:here is the updated code



Thanks for returning with the update. You earned a cow.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry much appreciated =))

I would also like to point out that I needed to change the first condition slightly(changing where current.previous points to)

reply
    Bookmark Topic Watch Topic
  • New Topic