C exercises and solutions: menu of choices


Circularly Linked List

Menu to operate circularly linked list

Step 8: 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(){

  printf("=================================\n");
  printf("Circularly Linked List Operations Menu\n");
  printf("=================================\n");
  printf("1.Add a new item\n");
  printf("2.Delete an item\n");
  printf("3.Show number of items\n");
  printf("4.Find min item\n");
  printf("5.Find max item\n");
  printf("6.Find item\n");
  printf("7.Print all items\n");
  printf("8.Exit\n");


}

void select(){
  int val, pos, ch;
  ListElem *temp;
  char yes='y';
  makeEmpty();//Make the list empty
  while(yes=='y'){
    printf("Enter your choice:");scanf("%d",&ch);
    switch(ch){
       case 1:
          printf("Value:");scanf("%d",&val);
          printf("Position:");scanf("%d",&pos);
          Insert(val,pos);
          break;
      case 2:
          printf("Position:");scanf("%d",&pos);
          Delete(pos);
          break;
     case 3:
          printf("Number of items:%d\n",countitem(pfirst));
          break;
     case 4:
         temp=findmin(pfirst);
         if(temp!=NULL)
            printf("The min item:%d\n",temp->data);
         else printf("Not found\n");
         break;
     case 5:
         temp=findmax(pfirst);
         if(temp!=NULL)
             printf("The max item:%d\n",temp->data);
         else printf("Not found\n");

         break;
     case 6:
         printf("Find what:");scanf("%d",&val);
         temp=find(pfirst,val);
         if(temp!=NULL)
            printf("The found item:%d\n",temp->data);
        else printf("Not found\n");
        break;
     case 7:
         printf("All items:\n");
         printall(pfirst);
         break;
     case 8: exit(0);

     default: printf("Invalid choice\n");

    }
       printf("Continue?y/n:");scanf("%s",&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.