Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Removing an element from a linked list..
New Posts  All Forums:Forum Nav:

Removing an element from a linked list..

post #1 of 2
Thread Starter 
Hey guys. In class we're on linked lists (Singly) right now, and I have to come up with a method that eliminates an element from a linked list. I get the logical part of having to go through the whole list until finding said element, then setting the previous one's link towards the one after the element to eliminate it. So far I'm having a bit of trouble. This is my code as of now, but its a bit messed up:
Code:
if (contains(element) == true) {
                        
                        
                        //if the element is the first one in the linkedBag
                        if (element.equals(currBag.getData())) {
                                bag = currBag.getNext();
                                size--;
                                return true;
                        }
                        else { // the element is not the first one
                                Node<E> previousBag = new Node<E>(bag.getData(), null);
                                currBag = bag.getNext();
                                Node<E> nextBag = bag.getNext().getNext();
                                int counter = 0;
                                while (counter < size) {
                                        
                                        if (element.equals(currBag.getData())) { 
                                                previousBag.setNext(nextBag); // remove the element by breaking the chain
                                                bag = previousBag;
                                                size--;
                                                return true; 
                                        }
                                        
                                        else { // not the first, or second...
//                                              Node<E> temp = new Node<E>(currBag.getData(), null);
                                                previousBag.setNext(currBag); // something's wrong here...
                                                currBag = new Node<E>(nextBag.getData(), null);
                                                nextBag = nextBag.getNext();
                                                counter++;
                                        }
                                        
                                } // end while current < size
                        }
                }
        return false;
}

Any and all advice is welcome!
 
My first rig!
(16 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 2500K SABERTOOTH P67 NVIDIA GeForce GTX 480 NVIDIA GeForce GTX 480 
RAMRAMHard DriveHard Drive
Corsair  Corsair  500GB HDD 7200rpm M4 Crucial 
Optical DriveCoolingOSMonitor
None Hyper 212 plus Win7 x64 Viewsonic "23 or "24 1080p 
Case
CoolerMaster HAF 912 
CPUMotherboardGraphicsRAM
FX 4100 MSI 970A-G45 EVGA GTX 460 8 GB Corsair Vengeance 1600 MHz 
Hard DriveOptical DriveCoolingCooling
500GB Hard Drive USB DVD/CD Corsair H50 Graphics card aftermarket cooler 
OSMonitorKeyboardPower
Win 7 x64 ViewSonic 24" Logitech 500W CoolerMaster 
CaseMouseAudioOther
NVIDIA Elite 334 LG Old speakers USB Lamp 
  hide details  
Reply
 
My first rig!
(16 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i5 2500K SABERTOOTH P67 NVIDIA GeForce GTX 480 NVIDIA GeForce GTX 480 
RAMRAMHard DriveHard Drive
Corsair  Corsair  500GB HDD 7200rpm M4 Crucial 
Optical DriveCoolingOSMonitor
None Hyper 212 plus Win7 x64 Viewsonic "23 or "24 1080p 
Case
CoolerMaster HAF 912 
CPUMotherboardGraphicsRAM
FX 4100 MSI 970A-G45 EVGA GTX 460 8 GB Corsair Vengeance 1600 MHz 
Hard DriveOptical DriveCoolingCooling
500GB Hard Drive USB DVD/CD Corsair H50 Graphics card aftermarket cooler 
OSMonitorKeyboardPower
Win 7 x64 ViewSonic 24" Logitech 500W CoolerMaster 
CaseMouseAudioOther
NVIDIA Elite 334 LG Old speakers USB Lamp 
  hide details  
Reply
post #2 of 2
You have the general right idea but your code is messy. If you first don't understand everything about the problem begin with pseudo code. It will cause you less frustration and you can use it as a model to easily code.
To remove a node from a linked list think of it in the following. (since this is for school I am not going to just give the answer)

currNode = head;
if currNode.data = key
update head
remove curr
else
previousNode = currNode
currNode = currNode.next
while < end
if currNode.data = key
previousNode.next = currNode.next
remove currNode
else
previousNode = currNode
currNode = currNode.next

Also your variable names should be more descriptive. There are also function calls that we have no clue what happens. Follow the pseudo code and see how it compares to yours. Also try cleaning up your code a little so it is a little cleaner for us to understand.

Good Luck
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Removing an element from a linked list..