Пример #1
0
        static void Main(string[] args)
        {
            DoublyLinkedList <Minion> minion = new DoublyLinkedList <Minion>
            {
                new Minion(1, "Kevin", 14, 3),
                new Minion(2, "Bob", 23, 2),
                new Minion(3, "Stuart", 21, 1)
            };

            minion.AddFirst(new Minion(4, "Mark", 20, 4));
            foreach (var item in minion)
            {
                Console.WriteLine(item);
            }

            minion.Remove(2);

            foreach (var t in minion.BackEnumerator())
            {
                Console.WriteLine(t);
            }
        }
        static void Main(string[] args)
        {
            DoublyLinkedList list = new DoublyLinkedList();

            list.AddLast(1);
            list.AddLast(2);
            list.AddLast(3);
            list.AddLast(4);
            list.AddLast(5);

            Console.WriteLine(string.Join(Environment.NewLine, list.ToArray()));

            //list.AddFirst(1);
            //list.AddFirst(2);
            //list.AddFirst(3);
            //list.AddFirst(4);
            //list.AddFirst(5);

            //Console.WriteLine(list.Count);
            //Console.WriteLine(list.Head.Value);
            //Console.WriteLine(list.Head.NextNode.Value);
        }
Пример #3
0
        public void printLinkedList(DoublyLinkedList dLL)
        {
            try
            {
                Node current = dLL.head.next;

                while (current != null && current.arbitrary != null && current.next != null)
                {
                    Console.WriteLine("Data Element:- " + current.data + ", Next Pointer:- " + current.next.data + ", Arbitrary Pointer:- " + current.arbitrary.data);
                    current = current.next;
                }

                if (current.next == null)
                {
                    Console.WriteLine("Data Element:- " + current.data + ", Next Pointer:- NULL, Arbitrary Pointer:- " + current.arbitrary.data);
                    Console.WriteLine("--------------------------------------------------------------");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException.ToString());
                Console.Read();
            }
        }
Пример #4
0
        public void createOiginalAndClonedLinkedList(DoublyLinkedList dLL, string input)
        {
            try
            {
                #region CreatingOriginalPointer

                string temp = null;
                while (input.Length > 0)
                {
                    temp = input.Substring(0, input.IndexOf(" "));
                    dLL.addToLast(temp);
                    input = input.Substring(input.IndexOf(" ") + 1);
                }

                dLL.createRandomPointers();

                Console.WriteLine("Original Linked List:-");
                printLinkedList(dLL);

                #endregion

                #region CreatingDuplicateElements

                current = dLL.head.next;

                while (current != null)
                {
                    Node newNode = new Node();
                    newNode.data = current.data;
                    newNode.next = current.next;
                    current.next = newNode;
                    current      = current.next.next;
                }

                Console.WriteLine("Intermediate Linked List:-");
                tempPrintLinkedList(dLL);

                #endregion

                #region AdjustingRandomPointers

                current = dLL.head.next;

                //Assigning arbitrary/random pointers of cloned Linked List
                while (current != null)
                {
                    current.next.arbitrary = current.arbitrary.next;
                    current = current.next.next;
                }

                #endregion

                #region RestoringOriginalLinkedList

                current = dLL.head.next;

                DoublyLinkedList clonedLL      = new DoublyLinkedList();
                Node             clonedCurrent = clonedLL.head;

                while (current != null)
                {
                    clonedCurrent.next = current.next;
                    current.next       = current.next.next;
                    current            = current.next;
                    clonedCurrent      = clonedCurrent.next;
                }

                Console.WriteLine("Cloned Linked List:-");
                printLinkedList(clonedLL);

                Console.WriteLine("Restored Original Linked List:-");
                printLinkedList(dLL);

                Console.Read();

                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException.ToString());
                Console.Read();
            }
        }
Пример #5
0
        public void Run()
        {
            SinglyLinkedList <int> sLinkedList = new SinglyLinkedList <int>();

            sLinkedList.PushToHead(7);

            sLinkedList.PushToHead(6);
            sLinkedList.PushToHead(5);
            sLinkedList.PushToHead(4);
            sLinkedList.PushToHead(3);
            sLinkedList.PushToHead(2);
            sLinkedList.PushToHead(1);

            sLinkedList.Print();
            sLinkedList.ReverseListWithGroupSizeRecursive(3);
            sLinkedList.Print();

            //Split original list into two nodes, dont allocate new list nodes
            //SinglyLinkedList<int> firstList = new SinglyLinkedList<int>();
            //SinglyLinkedList<int> secondList = new SinglyLinkedList<int>();
            //sLinkedList.AlternateSplitOfNodes(out firstList,out secondList);
            //firstList.Print();
            //secondList.Print();

            //sLinkedList.DeleteAlternateNodes();
            //sLinkedList.DeleteNodesAtDistanceRecursive(3);
            //sLinkedList.DeleteAlternateNodesRecursive();
            sLinkedList.DeleteNodesAtDistanceRecursive(3);
            sLinkedList.Print();


            SinglyLinkedList <int> sLinkedListOther = new SinglyLinkedList <int>();

            sLinkedListOther.PushToHead(5);
            sLinkedListOther.PushToHead(3);

            sLinkedListOther.Print();

            SinglyLinkedList <int> mergedIntersectionList = null;

            //mergedIntersectionList = sLinkedList.SortedIntersectionRecursive(sLinkedListOther);
            mergedIntersectionList = sLinkedList.SortedIntersectionIterative(sLinkedListOther);
            mergedIntersectionList.Print();


            Console.WriteLine("AreIdentical:" + sLinkedList.areIdentical(sLinkedListOther));
            Console.WriteLine("AreIdentical Recursive:" + sLinkedList.areIdenticalRecursive(sLinkedListOther));

            //sLinkedList.SortByNodeMovement();
            //sLinkedList.SortByDataMovement();
            sLinkedList.MergeSort();
            sLinkedList.Print();

            Console.WriteLine("Palindrome with stack: " + sLinkedList.IsPalindromeWithStack());
            Console.WriteLine("Palindrome without space: " + sLinkedList.IsPalindromeWithoutExtraSpace());
            Console.WriteLine("Palindrome with recursion: " + sLinkedList.IsPalindromeRecursive());

            sLinkedList.ReverseList();
            //sLinkedList.ReverseRecursive();

            sLinkedList.Print();

            Console.WriteLine("Count of 4: " + sLinkedList.countOccurances(4));
            SllNode <int> node1 = sLinkedList.GetNthNodeFromStart(2);
            SllNode <int> node2 = sLinkedList.GetNthNodeFromEnd(1);

            Console.WriteLine("Second node: " + node1.Data);
            Console.WriteLine("last node: " + node2.Data);

            //node2.Next = node1;
            Console.WriteLine("Loop:" + sLinkedList.IsLoopPresent());

            Console.WriteLine("Middle node: " + sLinkedList.GetMiddleNode().Data);
            sLinkedList.DeleteNode(node1);
            sLinkedList.Print();
            Console.WriteLine("Middle node: " + sLinkedList.GetMiddleNode().Data);


            Console.WriteLine("------Doubly linked list--------");

            DoublyLinkedList <int> doublyLinkedList = new DoublyLinkedList <int>();

            doublyLinkedList.PushToHead(1);
            doublyLinkedList.PushToHead(2);
            doublyLinkedList.PushToHead(3);
            doublyLinkedList.PushToHead(4);
            doublyLinkedList.PushToHead(5);
            doublyLinkedList.Print();
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            var slist = new SinglyLinkedList <int>();

            slist.Add(10);
            slist.Add(20);
            slist.Insert(2, 2);
            slist.Insert(2, 5);
            //slist.Remove(2);
            //slist.Insert(2, 3);

            slist.Add(0);
            slist.Add(1);
            slist.Add(2);
            Console.WriteLine("get index 1: " + slist.Get(1));
            //slist.Remove(2);
            //slist.Get(1);
            //slist.Remove(1);
            //slist.Get(0);
            //slist.Remove(0);

            var currentSItem = slist.First;

            while (currentSItem != null)
            {
                Console.WriteLine(currentSItem.Data);

                currentSItem = currentSItem.Next;
            }

            Console.WriteLine("Max index: " + slist.GetMaxIndex());
            Console.WriteLine("Max: " + slist.GetMax());
            Console.WriteLine("Total sum: " + slist.CumulatieveSum(slist.GetMaxIndex()));
            Console.WriteLine("Max recursive: " + slist.GetMaxRecursive());

            Console.WriteLine("next list");

            var dlist = new DoublyLinkedList <int>();

            /*dlist.Add(1);
             * dlist.Add(8);
             * dlist.Add(2);
             * dlist.Add(6);
             * dlist.Add(7);
             * dlist.Insert(2, 2);
             * dlist.Insert(2, 5);
             * dlist.Remove(2);
             * dlist.Insert(2, 3);*/

            dlist.Add(3);
            dlist.Add(2);
            dlist.Add(1);
            dlist.Add(3);
            dlist.Add(2);
            dlist.Add(1);

            var currentDItem = dlist.First;

            while (currentDItem != null)
            {
                Console.WriteLine(currentDItem.Data);

                currentDItem = currentDItem.Next;
            }

            Console.WriteLine("list ordered");

            dlist.InsertionSort();

            currentDItem = dlist.First;
            while (currentDItem != null)
            {
                Console.WriteLine(currentDItem.Data);

                currentDItem = currentDItem.Next;
            }

            Console.WriteLine("reverse index 2 and 5");

            dlist.Swap(2, 5);

            currentDItem = dlist.First;
            while (currentDItem != null)
            {
                Console.WriteLine(currentDItem.Data);

                currentDItem = currentDItem.Next;
            }

            Console.ReadKey();
        }
Пример #7
0
        static void Main(string[] args)
        {
            var doublyLinkedList = new DoublyLinkedList <int>();

            Console.WriteLine("Создана коллекция, пожалуйста введите значение типа int для добавления в начало списка:");
            var str = Console.ReadLine();

            //Заполнение
            while (true)
            {
                if (str == "w" || str == "q")
                {
                    break;
                }
                if (str == "c")
                {
                    Console.WriteLine(doublyLinkedList.Count);
                }
                if (str == "s")
                {
                    doublyLinkedList.ShowAll();
                }



                try
                {
                    doublyLinkedList.AddFirst(Convert.ToInt32(str));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Введенное значение не является командой или типом Int");
                }

                Console.WriteLine("Выход: q, Продолжить: w, Показать количество элементов: c, вывести все значения: s , Введите значение ");
                str = Console.ReadLine();
            }

            Console.WriteLine("Введите номер элемента, после которого удалить значение");
            str = Console.ReadLine();
            //Удаление
            while (true)
            {
                if (str == "q")
                {
                    break;
                }
                if (str == "c")
                {
                    Console.WriteLine(doublyLinkedList.Count);
                }

                if (str == "s")
                {
                    doublyLinkedList.ShowAll();
                }
                try
                {
                    doublyLinkedList.DeleteNext(Convert.ToInt32(str));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Введенное значение не является командой или типом Int");
                }
                Console.WriteLine("Выход: q,  Показать количество элементов: c, вывести все значения: s , Введите значение после которого удалить");
                str = Console.ReadLine();
            }

            Console.WriteLine("Программа завершена");
            Console.ReadLine();
        }