C++ exercises and solutions: Operations menu of Queue


Queue

Operations menu of Queue

Step 8: Displaying a menu of choices

To help the user feels friendly with your Queue data structure, you need to provide a list of choices to let the user interact with the program. This can be done by the following C++ code:

void showmenu(){

cout<<"=================================\n";
cout<<"Queue 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 and max items\n";
cout<<"5.Find an item\n";
cout<<"6.Show all items\n";
cout<<"7.Exit\n";


}

void select(){
 int val, ch;
 char yes='y';
 ListElem *del;
 while(yes=='y'){
 cout<<"Enter your choice:";cin>>ch;
 switch(ch){
  case 1:
    cout<<"Value:";cin>>val;
    Push(val);
    break;
  case 2:
   del=Pop();
   if(del!=NULL) cout<<"Deleted:"<<del->data<<endl;
   break;
  case 3:
   cout<<"Number of items:"<<countitem()<<endl;
   break;
  case 4:
    if(findmin()!=NULL && findmax()!=NULL){
     cout<<"Min item:"<<findmin()->data<<endl;
     cout<<"Max item:"<<findmax()->data<<endl;}
     break;
  case 5:
   cout<<"Find what?";cin>>val;
   if(find(val)!=NULL) cout<<"Found "<<find(val)->data<<endl;
   else cout<<"Not found\n";
   break;
  case 6:
    cout<<"All items:\n";
    printall();
    break;
  case 7: 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.