Hi ive made a code where a user makes a list and then displays it. he may also delete objects one by one.

suppose i wished to clear all in one go. How would i go about doing this? thank you in advance. my code is below. all works except the clear all option does nothing.

#include <iostream>
using namespace std;
struct node {
    string pro[10];
    string product;
    node *link;
};

node* buildListForward()
{
  node *first,*newNode, *last;
  string num;
  cout<<"\nEnter Products\nEnter 'quit' when done\n";
  cin >>num;

  first=NULL;
  while(num!="quit")
  {
    newNode = new node;     
    newNode->product =num;
    newNode->link = NULL;
    if(first==NULL)             
    {
    first = newNode;
    last= newNode;
    }
    else
    {
      last->link = newNode;
      last = newNode;
    }
        cin>>num;
   }
   return first;
}

node* deleteNode(node *head,string delItem)
{
  node *current,*prev;
  bool found=false;
  current = head;
  prev = head;
  while(current!=NULL && !found) {     
    if(current->product==delItem) found = true;
    else {
          prev = current;
          current = current->link;
     }
  }
  if(found) {
    if (current==head) head = current->link;
    else
     prev->link = current->link;
    delete current;
  }
  return head;
}

int main() {
    node *head, *current;
    string num;
    int pos;
    int menu;
    bool quit=false;
    do{ 
     cout<<"1.....Build List"<<endl;
     cout<<"2.....Delete Item"<<endl;
     cout<<"3.....Display"<<endl;
     cout<<"4.....Logout"<<endl;
     cout<<"5.....Quit"<<endl;
     cout << "Enter choice: ";
     cin >> menu;

     switch(menu){
            case 1:cout<< "\nBuild List"<<endl;
              head = buildListForward();
            system("cls");
                     break;

            case 2:cout<< "\nDelete Item"<<endl;
                cout<<"Enter number to delete\n";
                cin>>num;
                head = deleteNode(head,num);
             system("cls");
                     break;  

            case 3:cout<< "\nDisplay Item"<<endl;
            current = head;
            while(current!=NULL) {
            cout<<current->product<<endl;
            current = current->link;
            }
            system("pause");
            system("cls");
                     break;
            case 4:cout<<"Clear All";
            system("cls");
            break; 
            case 5:quit=true;
                     break;
            default:cout<<"Invalid entry must be 1..5"<<endl;
            }
    }while(!quit);    
    system("pause");
    return 0;
}

Line 83 is asking for the number of items to be deleted, not which product. So the string contains numeric digits. If the product name in the list is in fact numeric digits then the function might work if one of the products happens to be the same as the number of items you want to delete. You need to rethink how that deleteNode() is working.

Edited 4 Years Ago by Ancient Dragon

That is easy. Just set your head pointer to a null. But you will then have memory leak. So, if you want to avoid that, you have to travers the link list and free it then set your head pointer to null.

thankyou for your help. yes i had to change the delete node around and added an extra step in the switch so a user may delete all

This article has been dead for over six months. Start a new discussion instead.