Implementation Of Linked Lists Using Collections Name Space In C#

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. namespace LinkList {  
  4.     class Program {  
  5.         static LinkedList < int > ll = new LinkedList < int > ();  
  6.         static LinkedListNode < int > node;  
  7.         static void Main(string[] args) {  
  8.             Console.WriteLine(" LINKED LIST DEMO");  
  9.             int ch, x;  
  10.             Console.WriteLine("Linked List Operations");  
  11.             Console.WriteLine("1.AddFirst\n2.AddLast\n3.RemoveFirst\n4.RemoveLast\n5.Remove Specified\n6.Display\n7..Exit");  
  12.             while (true) {  
  13.                 Console.Write("Enter your Choice : ");  
  14.                 ch = Convert.ToInt16(Console.ReadLine());  
  15.                 switch (ch) {  
  16.                     case 1:  
  17.                         Console.Write("Enter the Element to AddFirst : ");  
  18.                         x = Convert.ToInt16(Console.ReadLine());  
  19.                         ll.AddFirst(x);  
  20.                         display();  
  21.                         break;  
  22.                     case 2:  
  23.                         Console.WriteLine("Enter the Element to AddLast : ");  
  24.                         x = Convert.ToInt16(Console.ReadLine());  
  25.                         ll.AddLast(x);  
  26.                         display();  
  27.                         break;  
  28.                     case 3:  
  29.                         if (ll.Count == 0) {  
  30.                             Console.WriteLine("Nothing to Delete...!!!");  
  31.                             break;  
  32.                         } else {  
  33.                             Console.WriteLine("First Element Removed Successfully");  
  34.                             ll.RemoveFirst();  
  35.                             display();  
  36.                             break;  
  37.                         }  
  38.                     case 4:  
  39.                         if (ll.Count == 0) {  
  40.                             Console.WriteLine("Nothing to Delete...!!!");  
  41.                             break;  
  42.                         } else {  
  43.                             Console.WriteLine("Last Element Removed Successfully");  
  44.                             ll.RemoveLast();  
  45.                             display();  
  46.                             break;  
  47.                         }  
  48.                     case 5:  
  49.                         if (ll.Count == 0) {  
  50.                             Console.WriteLine("Nothing to Delete...!!!");  
  51.                             break;  
  52.                         } else {  
  53.                             Console.WriteLine("Enter the Element to Remove");  
  54.                             x = Convert.ToInt16(Console.ReadLine());  
  55.                             bool b = ll.Remove(x);  
  56.                             if (b == true) {  
  57.                                 Console.WriteLine("Element Removed Successfully");  
  58.                                 display();  
  59.                                 break;  
  60.                             } else {  
  61.                                 Console.WriteLine("Specified Node Does not Exist");  
  62.                                 break;  
  63.                             }  
  64.                         }  
  65.                     case 6:  
  66.                         display();  
  67.                         break;  
  68.                     default:  
  69.                         Environment.Exit(0);  
  70.                         break;  
  71.                 }  
  72.             }  
  73.         }  
  74.         public static void display() {  
  75.             if (ll.Count == 0) {  
  76.                 Console.WriteLine("Nothing to Display...!!!");  
  77.             } else {  
  78.                 Console.Write("Elements in the List are:");  
  79.                 for (node = ll.First; node != null; node = node.Next) Console.Write(node.Value + " ");  
  80.                 Console.WriteLine();  
  81.             }  
  82.         }  
  83.     }  
  84. }