Example #1
0
        public void DisplayLinkedList()
        {
            Console.WriteLine("Select Question[1-18]");
            int choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                #region Single Linked List
            case 1:

                Console.WriteLine("--------1.Single Linked List--------");
                LinkedList <int> list = new LinkedList <int>();
                for (int i = 1; i <= 5; i++)
                {
                    list.Prepend(i);
                }
                list.InsertAfter(6, 5);
                list.InsertBefore(7, 3);
                list.PrintList();
                Console.WriteLine($"\nLength of List before delete {list.NodeCount()}");
                list.DelteNode(6);
                list.PrintList();
                Console.WriteLine($"\nLength of List after delete {list.NodeCount()}");

                LinkedList <string> list1 = new LinkedList <string>();
                string[]            str   = { "A", "B", "C", "D", "E" };
                foreach (var v in str)
                {
                    list1.Append(v);
                }
                list1.PrintList();
                list1.SearchNode("D");
                break;
                #endregion

                #region Double Linked List
            case 2:
                Console.WriteLine("--------2.Doubly Linked List--------");
                DoublyLinkedList <int> dlist = new DoublyLinkedList <int>();
                dlist.Append(40);
                dlist.Prepend(10);
                dlist.InsertAtMiddle(60, 1);
                dlist.InsertAtMiddle(70, 3);
                dlist.PrintList();
                dlist.Delete(3);
                dlist.NodeSearch(40);
                dlist.NodeCount();
                dlist.PrintList();
                break;

                #endregion

                #region Circular Linked List
            case 3:
                Console.WriteLine("--------3.Circular Linked List--------");
                CircularLinkedList <string> clist = new CircularLinkedList <string>();
                clist.Prepend("A");
                clist.Append("B");
                clist.Prepend("C");
                clist.Append("D");
                clist.InsertAt("E", 4);
                clist.NodeSearch("A");
                clist.PrintList();
                clist.NodeCount();
                clist.Delete("E");
                clist.PrintList();
                break;
                #endregion

                #region Detect loop Using HasSet
            case 4:
                Console.WriteLine("--------4.Detect loop Using HasSet--------");
                DetectLoopInLinkedListUsingHasSet <int> lop1 = new DetectLoopInLinkedListUsingHasSet <int>();
                lop1.Push(10);
                lop1.Push(20);
                lop1.Push(30);
                lop1.Push(40);
                lop1.Push(50);
                /*Create loop for testing */
                lop1.head.next.next.next.next = lop1.head;
                lop1.DetectLoop();
                break;
                #endregion

                #region Detect Loop With Brute Force Approach
            case 5:
                Console.WriteLine("--------5.Detect loop Using Brute Force Approach--------");
                DetectLoopInLinkedListUsingBF <string> lop2 = new DetectLoopInLinkedListUsingBF <string>();
                lop2.Insert("A");
                lop2.Insert("B");
                lop2.Insert("C");
                lop2.Insert("D");
                lop2.Insert("E");
                /*Create loop for testing */
                lop2.head.next.next.next = lop2.head;
                Console.WriteLine(lop2.DetectLoop());
                break;
                #endregion

                #region Detect Loop Using  Floyd’s Cycle
            case 6:
                Console.WriteLine("--------6.Detect loop Using Floyd’s Cycle--------");
                DetectLoopInLinkedListUsing_FloydCycle <int> lop3 = new DetectLoopInLinkedListUsing_FloydCycle <int>();
                lop3.push(40);
                lop3.push(30);
                lop3.push(20);
                lop3.push(10);
                /*Create loop for testing */
                lop3.head.next.next.next.next = lop3.head;
                lop3.DetectLoop();
                break;
                #endregion

                #region Palindrome Check Of Linked List Using Stack
            case 7:
                Console.WriteLine("--------7.Palindrome Check Of Linked List Using Stack--------");
                CheckLinkedListPalindromeUsingStack <int> pal1 = new CheckLinkedListPalindromeUsingStack <int>();
                pal1.Push(40);
                pal1.Push(30);
                pal1.Push(20);
                pal1.Push(30);
                pal1.Push(40);
                pal1.Print();
                pal1.CheckPalindrome();
                break;
                #endregion

                #region Reverse LinkedList
            case 8:
                Console.WriteLine("--------8.Reverse LinkedList--------");
                ReverseLinkedList <char> rev = new ReverseLinkedList <char>();
                char[] val = { 'a', 'b', 'c' };
                for (int i = 0; i < val.Length; i++)
                {
                    rev.Push(val[i]);
                }
                rev.PrintList();
                rev.ReverseList();
                Console.WriteLine("After Reverse");
                rev.PrintList();
                break;
                #endregion

                #region Reverse Linked List Using Recursion
            case 9:
                Console.WriteLine("--------9.Reverse Linked List Using Recursion--------");
                ReverseLinkedListUsingRecursion rev2 = new ReverseLinkedListUsingRecursion();
                rev2.Append(10);
                rev2.Append(20);
                rev2.Append(30);
                rev2.Print();
                rev2.Reverse();
                rev2.Print();
                break;
                #endregion

                #region Remove Duplicate From Sorted LinkedList
            case 10:
                Console.WriteLine("--------10.Remove Duplicate From Sorted LinkedList--------");
                RemoveDuplicateFromSortedLinkedList dct = new RemoveDuplicateFromSortedLinkedList();
                int[] arr = { 10, 10, 20, 30, 30, 40 };
                foreach (var i in arr)
                {
                    dct.Insert(i);
                }
                dct.Print();
                dct.RemoveDuplicate();
                dct.Print();
                break;
                #endregion

                #region Removing Duplicate From Unsorted LinkedList using Hashing
            case 11:
                Console.WriteLine("--------11.Removing Duplicate From Unsorted LinkedList using Hashing--------");
                RemovingDuplicateFromUnsortedLinkedList dct1 = new RemovingDuplicateFromUnsortedLinkedList();
                int[] lst = { 10, 30, 20, 30, 50, 10 };
                foreach (var i in lst)
                {
                    dct1.Push(i);
                }
                dct1.Print();
                dct1.RemoveDuplicate();
                dct1.Print();
                break;
                #endregion

                #region Print LinkedList From End
            case 12:
                Console.WriteLine("--------12.Print LinkedList From End--------");
                PrintLinkedListFromEnd p = new PrintLinkedListFromEnd();
                p.Print();
                break;
                #endregion

                #region Swap nodes in a linked list
            case 13:
                Console.WriteLine("--------13.Swap nodes in a linked list--------");
                SwapNodesInLinkedlist swa = new SwapNodesInLinkedlist();
                int[] ar13 = { 10, 12, 15, 16, 18, 20 };
                foreach (var v in ar13)
                {
                    swa.Push(v);
                }
                swa.PrintUsingRecursion(swa.head);
                Console.WriteLine();
                swa.Swap(12, 18);
                swa.Print();
                break;
                #endregion

                #region Reverse Double Linked List
            case 14:
                Console.WriteLine("--------14.Reverse Double Linked List--------");
                ReverseDoubleLinkedList rd = new ReverseDoubleLinkedList();
                int[] ar14 = { 10, 20, 30, 40 };
                foreach (var v in ar14)
                {
                    rd.Insert(v);
                }
                rd.Print();
                rd.Reverse();
                break;
                #endregion

                #region Reverse Pair Wise
            case 15:
                Console.WriteLine("--------15.Reverse Pair Wise--------");
                ReverseLinkedListPairWise l15 = new ReverseLinkedListPairWise();
                int[] ar15 = { 10, 20, 30, 40, 50 };
                foreach (var v in ar15)
                {
                    l15.Push(v);
                }
                l15.Print();
                l15.Reverse();
                l15.Print();
                break;
                #endregion

                #region Split circular linked List in 2 halfs
            case 16:
                Console.WriteLine("--------16.Split circular linked List in 2 halfs--------");
                SplitCircularLinkedList_inTwoParts o16 = new SplitCircularLinkedList_inTwoParts();
                int[] ar16 = { 50, 60, 20, 30, 13, 40, 56, 73, 10 };
                foreach (var v in ar16)
                {
                    o16.push(v);
                }
                o16.head.next.next.next.next.next.next.next.next.next = o16.head;
                o16.Print(o16.head);
                o16.SplitList(o16.head);
                o16.Print(o16.head1);
                o16.Print(o16.head2);
                break;
                #endregion

                #region Merge two Sorted Linked list
            case 17:
                Console.WriteLine("--------17.Merge two Sorted Linked list--------");
                Merge2SortedLinkedList l17 = new Merge2SortedLinkedList();
                l17.CreateList1();
                l17.CreateList2();
                l17.Print(l17.h1);
                l17.Print(l17.h2);
                l17.MegeList(l17.h1, l17.h2);
                l17.Print(l17.head);
                break;
                #endregion

                #region Plindrome check using Single linked list
            case 18:
                Console.WriteLine("--------" + choice + ".Merge two Sorted Linked list--------");
                CheckLinkedLisPalindromeUsingSingleLinkedList <int> l18 = new CheckLinkedLisPalindromeUsingSingleLinkedList <int>();
                l18.InsertFirst(1);
                l18.InsertFirst(2);
                l18.InsertFirst(2);
                l18.InsertFirst(1);
                l18.Print();
                l18.CheckPalindrome();
                break;
                #endregion

            default:
                Console.WriteLine("Oops! Invalid Choice.");
                break;
            }
        }