C# exercises solution-singly linked list menu


C# OOP: Singly Linked List

Menu to operate singly linked list

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:

 public static void showmenu(){

Console.WriteLine("=================================");
Console.WriteLine("Linked List Operations Menu");
Console.WriteLine("=================================");
Console.WriteLine("1.Add a new item");
Console.WriteLine("2.Delete an item");
Console.WriteLine("3.Show number of items");
Console.WriteLine("4.Show all items");
Console.WriteLine("5.Exit");

}


public static void select(){

LinkedList<int> mylist=new LinkedList<int>();
int val,ch, pos;
char yes = 'y';

while (yes == 'y')
{
Console.Write("Enter your choice:");
ch =int.Parse(Console.ReadLine().ToString ());

switch (ch)
{

case 1:
Console.Write("Value:");
val = int.Parse(Console.ReadLine());
Console.Write("Position:"); pos = int.Parse(Console.ReadLine());
mylist.insert(val, pos);
break;

case 2:
Console.Write("Position:"); pos = int.Parse(Console.ReadLine());
mylist.delete(pos);
break;

case 3:
Console.WriteLine("Number of items:" + mylist.countitem());
break;

case 4:
Console.WriteLine("All items:");
mylist.showall();
break;

case 5: Environment.Exit(0); break;

default: Console.WriteLine("Invalid choice!"); break;

}


Console.Write("Continue? Press y to continue:");
yes =char.Parse (Console.ReadLine());

}



}




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.