I NEED TO MAKE A PROGRAM BY USING THE LINKLIST IN C#

Mark Devid

Total Post:102

Points:714
Posted by  Mark Devid
C# 
C#
 787  View(s)
Ratings:
Rate this:

Hello Buddy

 i need to make a program by using the linklist in c# for following requirement

1. Adding the new string at the last of the Linkedlist

2. Adding the new string at the first of the Linkedlist

3. Adding the string after which do want to insert

4. Adding the string before which do want to insert

5. Deleting the 1st element

6. Deleting the last element

7. DIsplay

8. terminate the program

Please help me.I would rreally appreciate your help.

Thanks

  1. Abhishek Srivasatava

    Post:24

    Points:168
    Re: I need to make a program by using the linklist in c#

    Hello Devid,


    I have created the program as per your request. 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace Program
    {
        class DisplayMessageForInput
        {
            public string GetInputResult()
           
            {
                Console.WriteLine("Please enter the string");
                return Console.ReadLine();
            }
     
        }
        class Program
        {
            static void Main(string[] args)
            {
                DisplayMessageForInput objDisplayInput = new DisplayMessageForInput();
                int  opt = 0;
                LinkedList<string> link1= new LinkedList<string>();
                do
                {
                    Console.WriteLine("Main Menu");
                    Console.WriteLine("1. Adding the new string at the last of the Linkedlist");
                    Console.WriteLine("2. Adding the new string at the first of the Linkedlist");
                    Console.WriteLine("3. Adding the string after which do want to insert");
                    Console.WriteLine("4. Adding the string before which do want to insert");
                    Console.WriteLine("5. Deleting the 1st element");
                    Console.WriteLine("6. Deleting the last element");
                    Console.WriteLine("7. DIsplay");
                    Console.WriteLine("0. terminate the program");
     
                    Console.Write("Enter the Operation you want to perform : ");
                    opt = Convert.ToInt32(Console.ReadLine());
     
     
                    switch (opt)
                    {
                        case 1:
                        link1.AddLast(objDisplayInput.GetInputResult());
                            break;
                        case 2: link1.AddFirst(objDisplayInput.GetInputResult());
                            break;
                        case 3:
                            Console.WriteLine("Enter the string after which do want to insert");
                            string element = Console.ReadLine();
                            LinkedListNode<string> node =link1.Find(element);
                            link1.AddAfter(node,objDisplayInput.GetInputResult());
                            break;
                        case 4:
                            Console.WriteLine("Enter the string before which do want to insert");
                            string g = Console.ReadLine();
                            LinkedListNode<string> node1 = link1.Find(g);
                            link1.AddBefore(node1,objDisplayInput.GetInputResult());
                            break;
                      
                        case 5:
                            Console.WriteLine("Deleted element is {0} ",link1.First.Value);
                            link1.RemoveFirst();
                            break;
                        case 6:
                            Console.WriteLine("Deleted element is {0} ",link1.Last.Value);
                            link1.RemoveLast();
                            break;
                        case 7:
                            foreach (var linklist in link1)
                            {
                                Console.WriteLine("{0}", linklist);
                            }
                            break;
                        default:
                            if (opt != 0)
                                Console.WriteLine("Invalid option \n");
                            else
                                Console.WriteLine("End of the program \n ");
     
                            break;
                    }
                } while (opt != 0);
                Console.ReadLine();
            }
        }
    }

     

    Please, let us know if anything else required.




Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!