C# OOP: Stack

Menu of Stack

Step 8: Displaying a menu of choices

To help the user feels friendly with your Stack 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#(Csharp) code:

public static void select()
{

Stack<int> mylist = new Stack<int>();
ListNode<int> temp;
int val, ch;
char yes = 'y';
//display menu
showmenu();
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());
mylist.push(val);
break;

case 2:
temp = mylist.pop();
break;

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

case 4:
if (mylist.findmax() != null && mylist.findmin() != null)
Console.WriteLine("Min item:{0}\nMax item:{1}", mylist.findmin().val, mylist.findmax().val);
break;

case 5: Console.Write("Find what?");
val = int.Parse(Console.ReadLine());
temp = mylist.find(val);
if (temp != null) Console.WriteLine("Found {0}", temp.val);
else Console.WriteLine("Not found"); break;
case 6:
Console.WriteLine("All items:");
mylist.showall();
break;

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

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

}


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

}



}

public static void showmenu()
{

Console.WriteLine("=================================");
Console.WriteLine("Stack 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 min and max items");
Console.WriteLine("5.Find an item");
Console.WriteLine("6.Show all items");
Console.WriteLine("7.Exit");


}




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.