Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can anyone explain this
New Posts  All Forums:Forum Nav:

Can anyone explain this

post #1 of 5
Thread Starter 
So im studying for a final and im going over the algorithm and code for removing an item from a doubly linked list and i am confused on this line

current.getNext().setPrev(current.getNext());
and
current.getNext().setPrev(current.getPrev());

the whole remove method is as follows
Code:
public Node delete(int key){
Node current = first;

while(current.getData() != key){
current = current.getNext();
if(current == null)
return null;
}

if(current == first)
first = current.getNext();
else
current.getNext().setPrev(current.getNext()); 

if(current == last)
last = current.getPrev();
else
current.getNext().setPrev(current.getPrev()); 

size--;
return current;
i understand most of this but those 2 lines are confusing me
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #2 of 5
The first line takes the node after current and sets its prev to itself.
The second line takes the node after current and sets its prev to the node before current.

At least I think that's right. The first line confuses me a bit.
post #3 of 5
When working with linked lists diagrams are important.



I believe you're missing something as well. If the node isn't first or last then you'll still be left with a connection between current.getPrev() and current.

You need a current.getPrev().setNext(current.getNext()); in there too.
    
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X 
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS 
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700 
Audio
ASUS Xonar DG 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X 
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS 
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700 
Audio
ASUS Xonar DG 
  hide details  
Reply
post #4 of 5
Thread Starter 
this is just my version using private nodes with code from a book the book uses public nodes and it seems to work, although i do not doubt you as im sure you know much much more than i do also thank you for the diagram

the book uses
current.previous.next = current.next;
how do i read that from left to right or right to left??
is it setting currents previous next to currents next
or previous next current to the next current?
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #5 of 5
Quote:
Originally Posted by Sin Cities Sin View Post
this is just my version using private nodes with code from a book the book uses public nodes and it seems to work, although i do not doubt you as im sure you know much much more than i do also thank you for the diagram

the book uses
current.previous.next = current.next;
how do i read that from left to right or right to left??
is it setting currents previous next to currents next
or previous next current to the next current?
Left to right. current.previous is the node before current, so current.previous.next ends up referencing current.next after the assignment. Nodes don't have a 'current' variable, so your second option wouldn't work anyway.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can anyone explain this