By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,107 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,107 IT Pros & Developers. It's quick & easy.

Need a help in linked list

P: 4
Hello,

am a new member and this is my first posting. C++ is the first progrsaming language am taking and is just for 3 months. 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




Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>        //Creating a dynamic linked list 
  2. #include <iomanip.h>    
  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//modify the add function to insert at the back.)
  73.  {
  74.  
  75.      node *newrec = new node;
  76.  
  77.  
  78.      cout<<"Enter the a name: ";        //inserting infronth of a list
  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) // correct the function to stop hanging when an item that is not in the list is deleted
  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 ( 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. }
Mar 22 '07 #1
Share this Question
Share on Google+
1 Reply


Ganon11
Expert 2.5K+
P: 3,652
In your del() function, you are looping while the item has not been found by using strcmp() - you can add the condition

Expand|Select|Wrap|Line Numbers
  1. && current node isn't NULL
to keep it from trying to check past the end of the list.
Mar 22 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.