C++ exercises and solutions-Singly linked list menu

Singly linked list menu

Step 6: Display a menu of choices

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

void showmenu(){

cout<<"=================================\n";
cout<<"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 all items\n";
cout<<"5.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->Insert(val,pos);
break;
case 2:
cout<<"Position:";cin>>pos;
MyList->Delete(pos);
break;
case 3:
cout<<"Number of items:";cout<<MyList->countitem();
break;
case 4:
cout<<"All items:\n";
MyList->printall();
break;
case 5: exit(0);

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

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