| | |
Linked List Delete
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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
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
C++ Syntax (Toggle Plain Text)
bool list::del(Contributor Del) { Contributor infoIn=Del; node *pprev= plist; node *ptrav= plist; if (empty()) //empty list { cout<<"The list is empty "<<endl; } if (pprev==ptrav) //list beginning { plist = ptrav->next; //sends plist to next node delete ptrav; //delete Node } while((ptrav!=NULL) && (ptrav->infoIn!=Del)) { pprev= ptrav; ptrav= ptrav->next; } if (pprev==ptrav) return false; else if(pprev==NULL) { plist= plist->next; delete ptrav; } else { pprev->next =ptrav->next; delete ptrav; } return true; }
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 2
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.
Regards,
Rammohan Alampally,
<snip false signature>
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.
C++ Syntax (Toggle Plain Text)
struct node *DeleteNode ( struct node *p, int node_no ) { struct node *prev, *curr ; int i; if (p == NULL ) { printf("There is no node to be deleted \n"); } else { if ( node_no > length (p)) { printf("Error\n"); } else { prev = NULL; curr = p; i = 1 ; while ( i < node_no ) { prev = curr; curr = curr-> link; i = i+1; } if ( prev == NULL ) { p = curr -> link; free ( curr ); } else { prev -> link = curr -> link ; free ( curr ); } } } return(p); }
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
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
•
•
•
•
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
C++ Syntax (Toggle Plain Text)
bool list::del(Contributor Del) { Contributor infoIn=Del; node *pprev= plist; node *ptrav= plist; if (empty()) //empty list { cout<<"The list is empty "<<endl; } if (pprev==ptrav) //list beginning { plist = ptrav->next; //sends plist to next node delete ptrav; //delete Node } while((ptrav!=NULL) && (ptrav->infoIn!=Del)) { pprev= ptrav; ptrav= ptrav->next; } if (pprev==ptrav) return false; else if(pprev==NULL) { plist= plist->next; delete ptrav; } else { pprev->next =ptrav->next; delete ptrav; } return true; }
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.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
I commented, as the other code might prove overly long
C++ Syntax (Toggle Plain Text)
bool list::del(Contributor Del) { Contributor infoIn=Del; //Contributor is the object, infoIn is the specific object node *pprev= plist; //plist is the beginning of the list, ptrav is what some node *ptrav= plist; // would call "temp" or "head", and pprev is the "tail" if (empty()) // checking for empty list { cout<<"The list is empty "<<endl; } if (pprev==ptrav) //to delete from beginning of list { plist = ptrav->next; //sends plist to next node delete ptrav; //delete Node } while((ptrav!=NULL) && (ptrav->infoIn!=Del)) { pprev= ptrav; ptrav= ptrav->next; } //These functions should search for the // proper item to delete, and delete it if (pprev==ptrav) return false; else if(pprev==NULL) { plist= plist->next; delete ptrav; } else { pprev->next =ptrav->next; delete ptrav; } return true; }
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
Rejoice in the consistency of formatting no matter what editor or forum you post it on.
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
C++ Syntax (Toggle Plain Text)
bool list::del(Contributor Del) { Contributor infoIn=Del; // Contributor is the object, // infoIn is the specific object node *pprev= plist; // plist is the beginning of the list, // ptrav is what some node *ptrav= plist; // would call "temp" or "head", // and pprev is the "tail" if (empty()) // checking for empty list { cout<<"The list is empty "<<endl; } if (pprev==ptrav) //to delete from beginning of list { plist = ptrav->next; //sends plist to next node delete ptrav; //delete Node } while((ptrav!=NULL) && (ptrav->infoIn!=Del)) { pprev= ptrav; ptrav= ptrav->next; } //These functions should search for the // proper item to delete, and delete it if (pprev==ptrav) return false; else if(pprev==NULL) { plist= plist->next; delete ptrav; } else { pprev->next =ptrav->next; delete ptrav; } return true; }
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
•
•
•
•
C++ Syntax (Toggle Plain Text)
bool list::del(Contributor Del) { Contributor infoIn=Del; // Contributor is the object, // infoIn is the specific object node *pprev= plist; // plist is the beginning of the list, // ptrav is what some node *ptrav= plist; // would call "temp" or "head", // and pprev is the "tail"
It appears that after this code is executed, pprev, plist, and ptrav are all pointing to the same node.
•
•
•
•
C++ Syntax (Toggle Plain Text)
if (pprev==ptrav) //to delete from beginning of list { plist = ptrav->next; //sends plist to next node delete ptrav; //delete Node }
•
•
•
•
C++ Syntax (Toggle Plain Text)
while((ptrav!=NULL) && (ptrav->infoIn!=Del)) { pprev= ptrav; ptrav= ptrav->next; }
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.
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
This won't compile. contributor.cpp has this line:
You haven't provided this file.
C++ Syntax (Toggle Plain Text)
#include "Contributor.h"
You haven't provided this file.
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- Linked List (C++)
- Seg Fault ~ Linked List Delete (C)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Linked List (C++)
Other Threads in the C++ Forum
- Previous Thread: trying to print my Queue
- Next Thread: cirular arrys queue has me running in circles
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






