| | |
adding and deleting node in a linked list
![]() |
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 0
Hello,
am a new member and this is my first posting. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have written a program in c++ in linked list but am facing two problems. (1) when i delete an item that is not stored the program hangs and (2) I cannot modify the program to insert the node at the back i can only do it to insert at the front. here is the coding for my program
am a new member and this is my first posting. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have written a program in c++ in linked list but am facing two problems. (1) when i delete an item that is not stored the program hangs and (2) I cannot modify the program to insert the node at the back i can only do it to insert at the front. here is the coding for my program
c Syntax (Toggle Plain Text)
void del(node **record) { char name[20]; node *current, *previous; current = *record; cout<<"\n\nEnter name to delete: "; cin.getline(name,20); if ( strcmp(current->name,name) == 0) //if the target is the first node { *record = current->nextadd; delete(current); } else { while ( strcmp(current->name,name) != 0) { previous = current; current = current->nextadd; } previous->nextadd = current->nextadd; cout<<"\n\nDeleted"; delete(current); }
Last edited by WaltP; Mar 23rd, 2007 at 3:21 am. Reason: Added CODE tags, learn to use them please
>when i delete an item that is not stored the program hangs
That's because of this loop here:
If you go beyond the limits of your linked list, you're sending strcmp() a null pointer as one of the arguments. Not a good idea...
At this point, you have 2 options:
>I cannot modify the program to insert the node at the back i can only do it to insert at the front.
Show us the code.
That's because of this loop here:
C++ Syntax (Toggle Plain Text)
while ( strcmp(current->name,name) != 0)
At this point, you have 2 options:
- Add another condition to your while loop that ensures
currentis a valid pointer:C++ Syntax (Toggle Plain Text)- while ( current && ( strcmp(current->name, name) != 0 ) )
- Add an if() condition inside the while() loop to ensure that you don't go beyond the end of the linked list
current is valid to begin with.>I cannot modify the program to insert the node at the back i can only do it to insert at the front.
Show us the code.
Last edited by John A; Mar 23rd, 2007 at 12:19 am.
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 0
Here thanks for the quick and helpfull reply. ANd am sorry, i made a mistake in pasting the function for adding node. Anyway, You are a talented genius because you were still able to modify it to add at the front .....
Here is the complete code.

Here is the complete code.
c Syntax (Toggle Plain Text)
#include <iostream.h> //Creating a dynamic linked list #include <iomanip.h> //modify the add function to insert at the back. #include <string.h> #include <stdlib.h> struct node { char name[20]; int id_num; node *nextadd; }; void insert(node **); //function prototype void display( node * ); void del(node **); //function prototype node *list; main() { int i,option; char ans; bool cont = true; //two pointers to structure //get a pointer to the first structure list = NULL; //insert the current structure and create the remaining structures do { system("cls"); cout<<"\n\n1 : Add record"; cout<<"\n\n2 : Delete record"; cout<<"\n\n3 : Display record"; cout<<"\n\n4 : Exit"; cout<<"\n\n\nEnter your option: "; cin>>option; cin.get(); switch (option) { case 1: insert(&list); cont = true; break; case 2: del(&list); cont = true; break; case 3: display(list); cont = true; break; case 4: exit(1); cont = false; }; }while(true); } void insert(node **list) //record is pointer to a structure { node *newrec = new node; cout<<"Enter the a name: "; //modify the add function to insert at the back. cin.getline(newrec->name,20); cout<<"Enter the id number: "; cin>>newrec->id_num; cin.get(); newrec->nextadd = NULL; if( list== NULL) *list = newrec; else { newrec->nextadd = *list; *list = newrec; } } void display(node *contents) { if (contents == NULL) cout<<"\n\nList is empty"<<endl; else { while(contents != NULL) { cout<<setw(30)<<contents->name <<setw(20)<<contents->id_num<<endl; contents = contents->nextadd; } } cin.get(); } void del(node **record) { char name[20]; node *current, *previous; current = *record; cout<<"\n\nEnter name to delete: "; cin.getline(name,20); if ( strcmp(current->name,name) == 0) //if the target is the first node { *record = current->nextadd; delete(current); } else { while ( current && ( strcmp(current->name, name) != 0 ) ) { previous = current; current = current->nextadd; } previous->nextadd = current->nextadd; cout<<"\n\nDeleted"; delete(current); } }
Last edited by WaltP; Mar 24th, 2007 at 4:23 pm. Reason: Added CODE tags again. Do you see the words on the background of the post input box?
Some things you should take note of...
However, it's generally a better idea to use the "STL version" of them in a C++ program:
iostream.hand other STL headers with '.h' are outdated and deprecated. Replace them with this:C++ Syntax (Toggle Plain Text)- #include <iostream>
- #include <iomanip>
- using namespace std;
- You have all sorts of bad things happening with your input, not the least of which you're mixing cin and getline. Read this for more information.
- Try to avoid the use of system() calls, as it makes your code nonportable. Does the screen really have to be cleared for your program to work?
C++ Syntax (Toggle Plain Text)
#include <string.h> #include <stdlib.h>
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <cstdlib>
"Technological progress is like an axe in the hands of a pathological criminal."
>im sorry but whats a node i have never heard of that in my life b4
Then you've probably never heard of a linked list.
Then you've probably never heard of a linked list.
"Technological progress is like an axe in the hands of a pathological criminal."
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- Adding to linked list from external file (C)
- Why doesn't this remove the last node in a linked list? (C++)
Other Threads in the C++ Forum
- Previous Thread: catching errors
- Next Thread: connecting VC++ w/ MSSQL 2005
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets






