Coding Lab - TechOnTechnology

Program in C# to implement linked lists using the existing collections name space.


using System;
using System.Collections.Generic;
using System.Text;

namespace Linked_List
{
    class LinkedList
    {
        List lst;
        public LinkedList()
        {
            lst = new List();     // Create a list.
            Console.WriteLine("\n Initial number of elements in the LL : " + lst.Count);
        }
        public void insert()
        {
            Console.Clear();
            Console.WriteLine("\n *********************************************");
            Console.WriteLine("\n   1-----> INSERTION AT THE BEGINNING");
            Console.WriteLine("\n   2-----> INSERTION AT THE ENDING");
            Console.WriteLine("\n   3-----> INSERTION AFTER CERTAIN NODES");
            Console.WriteLine("\n *********************************************");
            Console.Write("\n\n Enter your choice: ");
            int ch = int.Parse(Console.ReadLine());
            string str;
            switch (ch)
            {
                case 1: Console.Write("\n Enter the data: ");
                        str=Console.ReadLine();
                        lst.Insert(0,str);
                        Console.WriteLine("\n {0} is inserted at the beginning.",str);
                        break;
                case 2: Console.Write("\n Enter the data: ");
                        str = Console.ReadLine();
                        lst.Add(str);
                        Console.WriteLine("\n {0} is inserted at the ending.",str);
                        break;
                case 3: if (lst.Count == 0)
                            {
                                Console.WriteLine("\n List is not built.");
                            }
                            else
                            {
                                Console.Write("\n After how many nodes? : ");
                                int n = int.Parse(Console.ReadLine());
                                if(n>=1&n
                                {
                                    Console.Write("\n Enter the data: ");
                                    str = Console.ReadLine();
                                    lst.Insert(n,str);
                                    Console.WriteLine("\n {0} is inserted after {1} nodes.", str, n);
                                }
                                else
                                    Console.WriteLine("\n After {0} nodes, insertion is not possible !",n);
                            }
                        break;
                default: Console.WriteLine("\n Sorry !!! Wrong  choice.");
                         break;
            }       
          }
        public void delete()
        {
            if (lst.Count == 0)
            {
                Console.WriteLine("\n LL is empty !");
            }

            else
            {

                Console.Clear();
                Console.WriteLine("\n *********************************************");
                Console.WriteLine("\n   1-----> DELETION (FIRST OCCURENCE)");
                Console.WriteLine("\n   2-----> DELETION  AT A SPECIFIED POSITION");
                Console.WriteLine("\n   3-----> DELETION OF ALL THE ELEMENTS");
                Console.WriteLine("\n *********************************************");
                Console.Write("\n\n Enter your choice: ");
                int ch = int.Parse(Console.ReadLine());
                switch (ch)
                {
                    case 1: Console.Write("\n Enter the data: ");
                            string str = Console.ReadLine();
                            if (lst.Contains(str))
                            {
                                lst.Remove(str);
                                Console.WriteLine("\n First occurence of {0} is deleted.", str);
                            }
                            else
                                Console.WriteLine("\n The data '{0}' doesn't exist.", str);
                            break;
                    case 2: Console.Write("\n Enter the position : ");
                            int n = int.Parse(Console.ReadLine());
                            if (n >= 1 & n
                            {
                                lst.RemoveAt(n - 1);
                                Console.WriteLine("\n Element at the position {0} is deleted.", n);
                            }                             
                            else
                                Console.WriteLine("\n Not a valid position to delete !");
                            break;
                    case 3: lst.Clear();
                            Console.WriteLine("\n All the elements are deleted");
                            break;
                    default: Console.WriteLine("\n Sorry !!! Wrong  choice.");
                            break;
                }
            }
        }
        public void status()
        {
            if (lst.Count == 0)
            {
                Console.WriteLine("\n LL is empty !");
            }
            else
            {
                Console.WriteLine("\n LL contains {0} elements.", lst.Count);
                Console.Write("\n The elements are : ");
                for (int i = 0; i < lst.Count; i++)
                    Console.Write(lst[i] + "\t");
            }
        }
        public void update()
        {
            if (lst.Count == 0)
            {
                Console.WriteLine("\n LL is empty !");
            }
            else
            {
                Console.Write("\n Enter the position of the element to update : ");
                int i = int.Parse(Console.ReadLine());
                if (i >= 1 & i
                {
                    Console.Write("\n Enter the data to update at the position {0} : ", i);
                    string str = Console.ReadLine();
                    lst[i-1] = str;
                    Console.WriteLine("\n Data at position {0} is changed to {1}", i, str);
                }
                else
                    Console.WriteLine("\n Invalid position ! ");
             }
        }
    }
    class GenListDemo
    {
        static void Main(string[] args)
        {
            LinkedList LL = new LinkedList();
            int i=1, ch;
            while(i==1)
            {
                Console.Clear();
                Console.WriteLine("\n ************************************");
                Console.WriteLine("\n   Welcome to LINKED LIST Operations.");
                Console.WriteLine("\n ************************************");
                Console.WriteLine("\n 1-----> INSERTION");
                Console.WriteLine("\n 2-----> DELETION");
                Console.WriteLine("\n 3-----> UPDATE");
                Console.WriteLine("\n 4-----> STATUS");
                Console.WriteLine("\n 5-----> EXIT");
                Console.WriteLine("\n ************************************");
                Console.Write("\n\n Enter your choice: ");
                ch = int.Parse(Console.ReadLine());
                switch (ch)
                {
                    case 1: LL.insert();
                            break;
                    case 2: LL.delete();
                            break;
                    case 3: LL.update();
                            break;
                    case 4: LL.status();
                            break;
                    case 5: Environment.Exit(-1);
                            break;
                    default: Console.WriteLine(" Sorry !!! Wrong  choice.");
                             break;
                }
                Console.Write("\n\n Press ENTER to Continue: ");
                Console.ReadLine();
            }
            Console.WriteLine("\n End of LINKED LIST operations... Bye");
        }
    }
}

Name

Email *

Message *