Search the web
Sign In
New User? Sign Up
cprogramming2 · C Programming Turning caffeine into code
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Linked list - deleting   Message List  
Reply | Forward Message #8563 of 8812 |
Re: Linked list - deleting

--- In cprogramming2@yahoogroups.com, Shivakumar <cshiva.in@...> wrote:
>
> Hi Everyone,
>
> I running into a few problems when I try to delete a node
> from a linked list.
>
> I am pasting the small portion of the code.
>
> void remove_product(int pid)
> {
> node *ptr, *temp, *temp2;
> ptr = start;
> while(ptr->next!=NULL)
> {
> if(ptr->product_id==pid)
> {
>
> //For deleting at the beginning of list
> if(ptr==start)
> {
> start = start->next;
> delete ptr;
> return;
> }
>
> //For deleting at the end of list
> if(ptr==end)
> {
> temp = end;

You've forgotten to set
end = end -> prev;
at this point before continuing.

> temp = temp->prev;
> temp->next = NULL;
> delete ptr;
> }
>
> //For deleting in the middle
> temp2 = ptr;
> ptr = ptr->next;
>
> temp2->next = ptr->next;
> delete ptr;

Completely wrong. You need:
temp2 = ptr -> prev;
temp2 -> next = ptr -> next;
ptr -> next -> prev = temp2;
delete ptr;

Don't get my "harsh" wording wrong, such mistakes are completely
normal when dealing with doubly linked lists for the first time or
after you haven't done that for a while.

> }
> }
>
> }
>
> int main(int argc, char *argv[])
> {
>
> if((chr == 'c' || chr == 'C'))
> {
> cout<<"Enter the product id"<<endl;
> cin>>ppid;
> i1->remove_product(ppid);
> }
>
> }
>
>
> deleting from the start of the list works fine, but deleting
> the middle and the end of the list is not happening. Could
> anyone please suggest what is the mistake here?

Regards,
Nico




Tue Dec 9, 2008 8:25 pm

nico_heinze
Offline Offline
Send Email Send Email

Forward
Message #8563 of 8812 |
Expand Messages Author Sort by Date

Hi Everyone, I running into a few problems when I try to delete a node from a linked list. I am pasting the small portion of the code. void remove_product(int...
Shivakumar
cshivain2000
Offline Send Email
Dec 9, 2008
1:11 pm

... You've forgotten to set end = end -> prev; at this point before continuing. ... Completely wrong. You need: temp2 = ptr -> prev; temp2 -> next = ptr ->...
Nico Heinze
nico_heinze
Offline Send Email
Dec 9, 2008
8:25 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help