C++ OOP: doubly linked list

Doubly linked list menu

Step 8: Display a menu of choices

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

    public static void select()

        {

            DoublyLinkedList<int> mylist = new DoublyLinkedList<int>();

            ListNode<int> temp;

            int val, ch, pos;

            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());

                        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:

                        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.printlist();

                        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("Doubly LinkedList 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.