![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
Linked List problem - Deleting a node
I have created a simple Linked List. The list stores a username, a IP address associated with the username, and a socket associated with the username. Most of the functions I have created for it work fine except the del() function. I wanted this function to delete a node, but it doesn't delete the node. Could someone show me where I went wrong?
Using OpenWatcom compiler on Windows XP. Here is the code: c Syntax (Toggle Plain Text)
|
|
|
|
|
|
#2 | |
|
hi: for(;;) goto hi;
|
Re: Linked List problem - Deleting a node
Quote:
Tip: This is the C forum.
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 747
Rep Power: 3
![]() |
Re: Linked List problem - Deleting a node
The textbook response to linked list problems is to draw it out on paper, then walk through your code on said paper and see what happens. Your problem is fairly easy if you're looking at a picture. Hint: you're breaking the chain instead of removing a link.
Side note about style: using a global variable for current_ptr is a poor practice. Your code would be better if you declared a local variable in each function where you iterate over the list.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Oct 2007
Posts: 39
Rep Power: 0
![]() |
Re: Linked List problem - Deleting a node
Yeah, and he posted C code, didn't he? And he's compiling with the Open Watcom C++/Fortran compiler.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Linked List problem - Deleting a node
peaceofpi was most likely referring to the author's use of cout (part of the C++ standard) and the "new" operator (C++ only), among other possible inconsistencies with C.
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Nov 2007
Posts: 33
Rep Power: 0
![]() |
Re: Linked List problem - Deleting a node
> Could someone show me where I went wrong?
Well you had a linked list which looked like this +=====+ +=====+ +=====+ +=====+ +=====+ | A |----->| B |----->| C |----->| D |----->| E |----->NULL +=====+ +=====+ +=====+ +=====+ +=====+ What you should end up with, after deleting 'D' is +=====+ +=====+ +=====+ +=====+ | A |----->| B |----->| C |----->| E |----->NULL +=====+ +=====+ +=====+ +=====+ But instead, you've got +=====+ +=====+ +=====+ | A |----->| B |----->| C |----->junk +=====+ +=====+ +=====+ You can't just blow away the node you want to delete, you need to repair the pointer chain to jump over the node you're going to delete, before you delete it. You also need some extra care when the node being deleted is also the head node in the list.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
Re: Linked List problem - Deleting a node
Sorry, Salem, you missed the real problem ...
There is also a problem that will emerge when creating the list. head_ptr is never initialised so it points to anything valid (i.e. a data structure). The first call to add_user will therefore introduce undefined behaviour. The code that creates a struct pointed at by current is fine, although it stores the address of the created struct in a local variable. Then move_current_to_end() is called, which does; current_ptr=head_ptr;
while(current_ptr->next!=NULL)
{
current_ptr=current_ptr->next;
} |
|
|
|
|
|
#8 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
Re: Linked List problem - Deleting a node
Ok, I have revised the code a bit, and I still having problems with the del() function.
c++ Syntax (Toggle Plain Text)
|
|
|
|
|
|
#9 |
|
Programmer
Join Date: Oct 2007
Posts: 39
Rep Power: 0
![]() |
Re: Linked List problem - Deleting a node
You need to keep track of the last_ptr. Then when you delete, last_ptr.next is loaded with current_ptr.next before you delete current_ptr.
|
|
|
|
|
|
#10 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
Re: Linked List problem - Deleting a node
Thanks for all of the help, I finally got it to work. Here is the code:
c++ Syntax (Toggle Plain Text)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| linked list problems | bl00dninja | C++ | 6 | Feb 17th, 2008 10:30 AM |
| linked list keep getting extra node | kruptof | C++ | 10 | Dec 22nd, 2006 10:44 PM |
| Linked List problem | Berto | C | 3 | Apr 4th, 2005 6:24 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |
| string problem when passing in linked list | quantz | C++ | 0 | Feb 27th, 2005 10:11 AM |