Linked List Delete

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Linked List Delete

 
0
  #1
Aug 21st, 2008
I'm having trouble getting my delete function to work for a singly linked list. If I try to delete anything other than the entire list, it deletes everything up to that node.

I know there is something wrong with it, but I can't tell what it is. I would like to structure it so that it can delete any one node in the list, then I'd like to be able to print the list to the screen. btw, insert and print work fine, just not the delete. Here is the code

  1. bool list::del(Contributor Del)
  2. {
  3. Contributor infoIn=Del;
  4.  
  5. node *pprev= plist;
  6. node *ptrav= plist;
  7.  
  8. if (empty()) //empty list
  9. {
  10. cout<<"The list is empty "<<endl;
  11. }
  12.  
  13. if (pprev==ptrav) //list beginning
  14. {
  15.  
  16. plist = ptrav->next; //sends plist to next node
  17. delete ptrav; //delete Node
  18. }
  19.  
  20. while((ptrav!=NULL) && (ptrav->infoIn!=Del))
  21. {
  22. pprev= ptrav;
  23. ptrav= ptrav->next;
  24. }
  25.  
  26. if (pprev==ptrav)
  27. return false;
  28. else
  29. if(pprev==NULL)
  30. {
  31. plist= plist->next;
  32. delete ptrav;
  33. }
  34. else
  35. {
  36. pprev->next =ptrav->next;
  37. delete ptrav;
  38. }
  39.  
  40. return true;
  41. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: findsyntax has a little shameless behaviour in the past 
Solved Threads: 2
findsyntax findsyntax is offline Offline
Newbie Poster

Re: Linked List Delete

 
0
  #2
Aug 21st, 2008
Hi,
I am Rammohan from Bangalore and working as a Technical lead in big IT firm .

Solution for your answer is follows:

The way you have written is only for the deleting a node. For that also you have not verified weather the node is exist in the list or not.
Please have a look at my style fo code and use it where ever you want.

  1. struct node *DeleteNode ( struct node *p, int node_no )
  2. {
  3. struct node *prev, *curr ;
  4. int i;
  5. if (p == NULL )
  6. {
  7. printf("There is no node to be deleted \n");
  8. }
  9. else
  10. {
  11. if ( node_no > length (p))
  12. {
  13. printf("Error\n");
  14. }
  15. else
  16. {
  17. prev = NULL;
  18. curr = p;
  19. i = 1 ;
  20. while ( i < node_no )
  21. {
  22. prev = curr;
  23. curr = curr-> link;
  24. i = i+1;
  25. }
  26. if ( prev == NULL )
  27. {
  28. p = curr -> link;
  29. free ( curr );
  30. }
  31. else
  32. {
  33. prev -> link = curr -> link ;
  34. free ( curr );
  35. }
  36. }
  37. }
  38. return(p);
  39. }

Regards,
Rammohan Alampally,
<snip false signature>
Last edited by Ancient Dragon; Aug 21st, 2008 at 7:45 am. Reason: snip false signature and add code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Linked List Delete

 
0
  #3
Aug 21st, 2008
Originally Posted by henpecked1 View Post
I'm having trouble getting my delete function to work for a singly linked list. If I try to delete anything other than the entire list, it deletes everything up to that node.

I know there is something wrong with it, but I can't tell what it is. I would like to structure it so that it can delete any one node in the list, then I'd like to be able to print the list to the screen. btw, insert and print work fine, just not the delete. Here is the code

  1. bool list::del(Contributor Del)
  2. {
  3. Contributor infoIn=Del;
  4.  
  5. node *pprev= plist;
  6. node *ptrav= plist;
  7.  
  8. if (empty()) //empty list
  9. {
  10. cout<<"The list is empty "<<endl;
  11. }
  12.  
  13. if (pprev==ptrav) //list beginning
  14. {
  15.  
  16. plist = ptrav->next; //sends plist to next node
  17. delete ptrav; //delete Node
  18. }
  19.  
  20. while((ptrav!=NULL) && (ptrav->infoIn!=Del))
  21. {
  22. pprev= ptrav;
  23. ptrav= ptrav->next;
  24. }
  25.  
  26. if (pprev==ptrav)
  27. return false;
  28. else
  29. if(pprev==NULL)
  30. {
  31. plist= plist->next;
  32. delete ptrav;
  33. }
  34. else
  35. {
  36. pprev->next =ptrav->next;
  37. delete ptrav;
  38. }
  39.  
  40. return true;
  41. }

I think you need to post more code or comment the code you have or something. We have no idea what these variables represent and what the classes are.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Linked List Delete

 
0
  #4
Aug 21st, 2008
I commented, as the other code might prove overly long

  1. bool list::del(Contributor Del)
  2. {
  3. Contributor infoIn=Del; //Contributor is the object, infoIn is the specific object
  4.  
  5. node *pprev= plist; //plist is the beginning of the list, ptrav is what some
  6. node *ptrav= plist; // would call "temp" or "head", and pprev is the "tail"
  7.  
  8. if (empty()) // checking for empty list
  9. {
  10. cout<<"The list is empty "<<endl;
  11. }
  12.  
  13. if (pprev==ptrav) //to delete from beginning of list
  14. {
  15.  
  16. plist = ptrav->next; //sends plist to next node
  17. delete ptrav; //delete Node
  18. }
  19.  
  20. while((ptrav!=NULL) && (ptrav->infoIn!=Del))
  21. {
  22. pprev= ptrav;
  23. ptrav= ptrav->next;
  24. } //These functions should search for the
  25. // proper item to delete, and delete it
  26. if (pprev==ptrav)
  27. return false;
  28. else
  29. if(pprev==NULL)
  30. {
  31. plist= plist->next;
  32. delete ptrav;
  33. }
  34. else
  35. {
  36. pprev->next =ptrav->next;
  37. delete ptrav;
  38. }
  39.  
  40. return true;
  41. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Linked List Delete

 
0
  #5
Aug 21st, 2008
Sheesh - 200 posts, and the indentation is still crap.

Your super IDE may be able to make sense of your crazy mix of spaces and tabs, but most everything else will (sooner or later) make a complete pig's ear of it.

Do everyone a favour and turn OFF tabs, and turn on smart indenting with spaces. It won't cost you any extra keystrokes, and it'll make for a much better experience all round.

Here, indented with spaces only
  1. bool list::del(Contributor Del)
  2. {
  3. Contributor infoIn=Del; // Contributor is the object,
  4. // infoIn is the specific object
  5.  
  6. node *pprev= plist; // plist is the beginning of the list,
  7. // ptrav is what some
  8. node *ptrav= plist; // would call "temp" or "head",
  9. // and pprev is the "tail"
  10.  
  11. if (empty()) // checking for empty list
  12. {
  13. cout<<"The list is empty "<<endl;
  14. }
  15.  
  16. if (pprev==ptrav) //to delete from beginning of list
  17. {
  18.  
  19. plist = ptrav->next; //sends plist to next node
  20. delete ptrav; //delete Node
  21. }
  22.  
  23. while((ptrav!=NULL) && (ptrav->infoIn!=Del))
  24. {
  25. pprev= ptrav;
  26. ptrav= ptrav->next;
  27. } //These functions should search for the
  28.  
  29. // proper item to delete, and delete it
  30. if (pprev==ptrav)
  31. return false;
  32. else
  33. if(pprev==NULL)
  34. {
  35. plist= plist->next;
  36. delete ptrav;
  37. }
  38. else
  39. {
  40. pprev->next =ptrav->next;
  41. delete ptrav;
  42. }
  43.  
  44. return true;
  45. }
Rejoice in the consistency of formatting no matter what editor or forum you post it on.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Linked List Delete

 
0
  #6
Aug 21st, 2008
My apologies for the formatting in my recent posts. I no longer have an IDE at work and must "troubleshoot" my errors in a text editor until I get home.

Unfortunately, it doesn't hold the formatting of an IDE well, and makes things even worse when it is copied and pasted into the forum.

I assure you my intent is not to confuse or irritate, nor bring your wrath down upon me. Your experience in this subject, and indeed on this board is vastly superior to mine and I only ask your patience since even after 200 posts, I am, by all standards, still a beginner in both.
Last edited by henpecked1; Aug 21st, 2008 at 5:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Linked List Delete

 
0
  #7
Aug 21st, 2008
  1. bool list::del(Contributor Del)
  2. {
  3. Contributor infoIn=Del; // Contributor is the object,
  4. // infoIn is the specific object
  5.  
  6. node *pprev= plist; // plist is the beginning of the list,
  7. // ptrav is what some
  8. node *ptrav= plist; // would call "temp" or "head",
  9. // and pprev is the "tail"
Contributor is NOT an object. It looks like Contributor is a class or a struct. What relationship it has to the node class/struct is not clear. plist appears to have a type of node* and points to the first node in the list.

It appears that after this code is executed, pprev, plist, and ptrav are all pointing to the same node.

  1. if (pprev==ptrav) //to delete from beginning of list
  2. {
  3.  
  4. plist = ptrav->next; //sends plist to next node
  5. delete ptrav; //delete Node
  6. }
Since ptrav and pprev will always point to the same node at this point, this if statement is unnecessary since it will always evaluate to true. plist now points to the second node in the list. Is this if statement supposed to ALWAYS execute? If not, I'd put some debugging statement inside of it that displays something and find out if it is ever skipped. It appears to me that it is never skipped.

  1.  
  2. while((ptrav!=NULL) && (ptrav->infoIn!=Del))
  3. {
  4. pprev= ptrav;
  5. ptrav= ptrav->next;
  6. }
You define infoIn as type Contributor above. It also appears to be a data member of the node class/struct. So does each node store data of type Contributor and infoIn is the Contributor member of the node class? The function is passed not the node to delete itself but rather the data that the node to delete contains? If you aren't careful, using the same name for both a specific class/struct object and a class/struct data member can get confusing. The infoIn object form this line:

Contributor infoIn=Del;

is never used as far as I can see, so can be deleted. It's different from the infoIn from this line:

while((ptrav!=NULL) && (ptrav->infoIn!=Del))


I think you need to provide at least the header of the Contributor, list, and node classes/structs. Not the method implementation code itself, but the header that lists at least what each struct/class's data members are and how they relate to each other. Posting the entire header with the method declarations included could be helpful.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Linked List Delete

 
0
  #8
Aug 21st, 2008
Vernon,

Here is the code you requested. I would have posted it all, but it seemed like it would be terribly long, so I attached it instead.
Attached Files
File Type: h ContributorClass.h (2.4 KB, 9 views)
File Type: h NodeClass.h (246 Bytes, 10 views)
File Type: h ListClass.h (279 Bytes, 10 views)
File Type: cpp NodeClass.cpp (168 Bytes, 7 views)
File Type: cpp ListClass.cpp (2.0 KB, 12 views)
File Type: cpp contributor.cpp (3.5 KB, 6 views)
File Type: cpp List Main.cpp (637 Bytes, 10 views)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Linked List Delete

 
0
  #9
Aug 22nd, 2008
This won't compile. contributor.cpp has this line:

  1. #include "Contributor.h"

You haven't provided this file.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Linked List Delete

 
0
  #10
Aug 22nd, 2008
bah...I attached the wrong cpp file. try this one

and it should be asking for ContributorClass.h no space, add a space, whichever...I caught it after I attached it
Last edited by henpecked1; Aug 22nd, 2008 at 12:13 am.
Attached Files
File Type: cpp Contributor class.cpp (4.9 KB, 11 views)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC