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

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);
}

>when i delete an item that is not stored the program hangs
That's because of this loop here:

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:
    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. ;)

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.

#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);
    }       
}

Some things you should take note of...

  • iostream.h and other STL headers with '.h' are outdated and deprecated. Replace them with this:
    #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?

Just so you know, these headers are NOT part of the STL, and are therefore not deprecated:

#include <string.h>
#include <stdlib.h>

However, it's generally a better idea to use the "STL version" of them in a C++ program:

#include <cstring>
#include <cstdlib>

im sorry but whats a node i have never heard of that in my life b4

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.