Answer to Question #129135 in C# for MD RASEL BHUYAN

Question #129135
WAP using switch case to perform following operations.
i) create a linked list,
ii) display its elements,
iii) count the number of nodes,
iv) search an element
v) sort the linked list
vi) reverse the elements of the list
1
Expert's answer
2020-08-11T10:40:34-0400
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;




namespace LinkedList

{

  class Program

  {

    static void Main(string[] args)

    {

      LinkedList<int> collection = new LinkedList<int>();

      while (true)

      {

        ShowMenu();

        int selection = ReadInteger("\nSelect number of option: ");




        switch (selection)

        {

          case 0:

            Console.WriteLine("Enter the sequance of numbers to create list: ");

            try

            {

              string input = Console.ReadLine();

              collection = new LinkedList<int>(input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)));

              Console.WriteLine("Created");

            }

            catch

            {

              Console.WriteLine("Incorrect input!");

            }

            break;

          case 1:

            Console.Write("\nCollection: ");

            foreach (var item in collection)

              Console.Write($"{item} ");

            Console.WriteLine();

            break;

          case 2:

            Console.WriteLine($"\nCount of nodes: {collection.Count}");

            break;

          case 3:

            int value = ReadInteger("\nEnter the number to find: ");

            var node = collection.Find(value);

            if (node == null)

            {

              Console.WriteLine("\nElement didn't find!");

            }

            else

            {

              Console.WriteLine($"\nNode was found with value: {node.Value}");

            }

            break;

          case 4:

            collection = Sort(collection);

            Console.WriteLine("Sorted");

            break;

          case 5:

            collection = new LinkedList<int>(collection.Reverse());

            Console.WriteLine("Reversed!");

            break;

          default:

            return;

        }

      }

    }




    static void ShowMenu()

    {

      Dictionary<int, string> menuItems = new Dictionary<int, string>()

      {

        {0, "Create Linked List" },

        {1, "Display elements" },

        {2, "Count the number of nodes" },

        {3, "Search an element" },

        {4, "Sort the linked list" },

        {5, "Reverse the elements of the list" },

        {6, "Exit" }

      };




      foreach (KeyValuePair<int, string> item in menuItems)

        Console.WriteLine($"{item.Key}. {item.Value}");

    }




    static LinkedList<int> Sort(LinkedList<int> linkedList)

    {

      LinkedList<int> values = new LinkedList<int>();




      while(linkedList.Count > 0)

      {

        int min = linkedList.Min();




        linkedList.Remove(min);

        values.AddLast(min);

      }




      return values;

    }




    static int ReadInteger(string message)

    {

      while (true)

      {

        Console.Write(message);

        if (int.TryParse(Console.ReadLine(), out int value) && value >= 0)

          return value;

        else

        {

          Console.WriteLine("Incorrect input.");

        }

      }

    }

  }

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS