C++ exercises and solutions-Doubly linked list menu


C++ OOP: Doubly Linked List

Doubly linked list menu

Step 8: Display a menu of choices

To show the menu that allow the user to choose an operation on the doubly linked list, you need the showmenu() and select() functions as shown below:

void showmenu(){

           cout<<"=================================\n";

  cout<<"Doubly Linked List Operations Menu\n";

        cout<<"=================================\n";

  cout<<"1.Add a new item\n";

  cout<<"2.Delete an item\n";

  cout<<"3.Show number of items\n";

  cout<<"4.Show min item\n";

  cout<<"5.Show max item\n";

  cout<<"6.Find an item\n";

  cout<<"7.Show all items\n";

  cout<<"8.Exit\n";

 

}

 

void select(){

  LinkedList<int> *MyList=new LinkedList<int>();

  int val, pos, ch;

  char yes='y';

  while(yes=='y'){

    cout<<"Enter your choice:";cin>>ch;

    switch(ch){

        case 1:

          cout<<"Value:";cin>>val;

          cout<<"Position:";cin>>pos;

          MyList->insertNode(val,pos);

          break;

         case 2:

          cout<<"Position:";cin>>pos;

          MyList->deleteNode(pos);

          break;

        case 3:

          cout<<"Number of items:";cout<<MyList->countitem()<<endl;

          break;

       case 4:

          MyList->temp=MyList->findmin();

          if(MyList->temp!=NULL)

                   cout<<"The min item:"<<MyList->temp->data<<endl;

          else cout<<"Not found\n";

          break;

        case 5:

          MyList->temp=MyList->findmax();

          if(MyList->temp!=NULL)

                   cout<<"The max item:"<<MyList->temp->data<<endl;

          else cout<<"Not found\n";

          break;

        case 6:

          cout<<"Find what:";

          cin>>val;

          MyList->temp=MyList->find(val);

          if(MyList->temp!=NULL) cout<<"Found:"<<MyList->temp->data<<endl;

          else cout<<"Not found!"<<endl;

          break;

        case 7:

          cout<<"All items:\n";

          MyList->printall();

          break;

        case 8: exit(0);

        default: cout<<"Invalid choice\n";

    }

      cout<<"Continue?y/n:";cin>>yes;

  }

}




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.