adding and deleting node in a linked list

Reply

Join Date: Mar 2007
Posts: 2
Reputation: dabbakal is an unknown quantity at this point 
Solved Threads: 0
dabbakal dabbakal is offline Offline
Newbie Poster

adding and deleting node in a linked list

 
0
  #1
Mar 22nd, 2007
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
  1. void del(node **record)
  2. {
  3. char name[20];
  4.  
  5. node *current, *previous;
  6.  
  7. current = *record;
  8.  
  9.  
  10. cout<<"\n\nEnter name to delete: ";
  11. cin.getline(name,20);
  12.  
  13.  
  14. if ( strcmp(current->name,name) == 0) //if the target is the first node
  15. {
  16. *record = current->nextadd;
  17.  
  18. delete(current);
  19. }
  20.  
  21. else
  22. {
  23.  
  24.  
  25. while ( strcmp(current->name,name) != 0)
  26. {
  27. previous = current;
  28. current = current->nextadd;
  29.  
  30. }
  31.  
  32. previous->nextadd = current->nextadd;
  33. cout<<"\n\nDeleted";
  34. delete(current);
  35. }
Last edited by WaltP; Mar 23rd, 2007 at 3:21 am. Reason: Added CODE tags, learn to use them please
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: adding and deleting node in a linked list

 
0
  #2
Mar 23rd, 2007
>when i delete an item that is not stored the program hangs
That's because of this loop here:
  1. while ( strcmp(current->name,name) != 0)
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:
  • Add another condition to your while loop that ensures current is a valid pointer:
    1. 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
Doing the former is probably better, because it works whether or not 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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: dabbakal is an unknown quantity at this point 
Solved Threads: 0
dabbakal dabbakal is offline Offline
Newbie Poster

Re: adding and deleting node in a linked list

 
0
  #3
Mar 24th, 2007
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.

  1. #include <iostream.h> //Creating a dynamic linked list
  2. #include <iomanip.h> //modify the add function to insert at the back.
  3.  
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. struct node {
  8.  
  9. char name[20];
  10. int id_num;
  11. node *nextadd;
  12. };
  13.  
  14. void insert(node **); //function prototype
  15. void display( node * );
  16. void del(node **); //function prototype
  17.  
  18.  
  19. node *list;
  20.  
  21. main()
  22. {
  23. int i,option;
  24. char ans;
  25. bool cont = true;
  26. //two pointers to structure
  27.  
  28.  
  29.  
  30. //get a pointer to the first structure
  31.  
  32. list = NULL;
  33.  
  34.  
  35.  
  36.  
  37.  
  38. //insert the current structure and create the remaining structures
  39. do {
  40. system("cls");
  41. cout<<"\n\n1 : Add record";
  42. cout<<"\n\n2 : Delete record";
  43. cout<<"\n\n3 : Display record";
  44. cout<<"\n\n4 : Exit";
  45.  
  46. cout<<"\n\n\nEnter your option: ";
  47. cin>>option;
  48. cin.get();
  49.  
  50. switch (option) {
  51.  
  52. case 1: insert(&list);
  53. cont = true;
  54. break;
  55. case 2: del(&list);
  56. cont = true;
  57. break;
  58. case 3: display(list);
  59. cont = true;
  60. break;
  61. case 4: exit(1);
  62.  
  63. cont = false;
  64. };
  65.  
  66. }while(true);
  67.  
  68. }
  69.  
  70.  
  71.  
  72. void insert(node **list) //record is pointer to a structure
  73. {
  74.  
  75. node *newrec = new node;
  76.  
  77.  
  78. cout<<"Enter the a name: "; //modify the add function to insert at the back.
  79. cin.getline(newrec->name,20);
  80. cout<<"Enter the id number: ";
  81. cin>>newrec->id_num;
  82. cin.get();
  83.  
  84. newrec->nextadd = NULL;
  85.  
  86.  
  87. if( list== NULL)
  88.  
  89. *list = newrec;
  90.  
  91. else
  92. {
  93. newrec->nextadd = *list;
  94. *list = newrec;
  95. }
  96. }
  97.  
  98.  
  99.  
  100. void display(node *contents)
  101.  
  102. {
  103.  
  104. if (contents == NULL)
  105. cout<<"\n\nList is empty"<<endl;
  106. else
  107. {
  108.  
  109. while(contents != NULL)
  110. {
  111. cout<<setw(30)<<contents->name
  112. <<setw(20)<<contents->id_num<<endl;
  113. contents = contents->nextadd;
  114. }
  115. }
  116.  
  117. cin.get();
  118. }
  119.  
  120.  
  121.  
  122. void del(node **record)
  123. {
  124. char name[20];
  125.  
  126. node *current, *previous;
  127.  
  128. current = *record;
  129.  
  130.  
  131. cout<<"\n\nEnter name to delete: ";
  132. cin.getline(name,20);
  133.  
  134.  
  135. if ( strcmp(current->name,name) == 0) //if the target is the first node
  136. {
  137. *record = current->nextadd;
  138.  
  139. delete(current);
  140. }
  141.  
  142. else
  143. {
  144.  
  145.  
  146. while ( current && ( strcmp(current->name, name) != 0 ) )
  147. {
  148. previous = current;
  149. current = current->nextadd;
  150.  
  151. }
  152.  
  153. previous->nextadd = current->nextadd;
  154. cout<<"\n\nDeleted";
  155. delete(current);
  156. }
  157. }
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: adding and deleting node in a linked list

 
0
  #4
Mar 24th, 2007
Some things you should take note of...
  • iostream.h and other STL headers with '.h' are outdated and deprecated. Replace them with this:
    1. #include <iostream>
    2. #include <iomanip>
    3. 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?
Just so you know, these headers are NOT part of the STL, and are therefore not deprecated:
  1. #include <string.h>
  2. #include <stdlib.h>
However, it's generally a better idea to use the "STL version" of them in a C++ program:
  1. #include <cstring>
  2. #include <cstdlib>
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 163
Reputation: grunge man is an unknown quantity at this point 
Solved Threads: 2
grunge man grunge man is offline Offline
Junior Poster

Re: adding and deleting node in a linked list

 
0
  #5
Mar 25th, 2007
im sorry but whats a node i have never heard of that in my life b4
THE ONLY WAY TO MOVE IN THE FUTURE IS TO ABSTRACT IN THE PAST
(My interpretation of abstract, is, to change something little by little until it morphs into something new)
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: adding and deleting node in a linked list

 
0
  #6
Mar 25th, 2007
>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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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