示例#1
0
        static void Main(string[] args)
        {
            DoublyLinkedList list = new DoublyLinkedList();
            Console.WriteLine("Insert 1, 2, 3, 4");
            list.Insert(1);
            list.Insert(2);
            list.Insert(3);
            list.Insert(4);
            list.Print();

            list.InsertAt(0, 77);
            list.InsertAt(4, 77);
            list.Print();

            Console.ReadKey();
        }
示例#2
0
        static void Main(string[] args)
        {
            DoublyLinkedList <int> list = new DoublyLinkedList <int>();

            list.AddFirst(112);
            list.AddFirst(911);
            list.AddFirst(166);
            list.AddFirst(160);
            list.AddFirst(150);

            Console.WriteLine(string.Join(" ", list));
            Console.WriteLine($"Count = {list.Count}");

            Console.WriteLine(list.RemoveFirst());
            Console.WriteLine(list.RemoveFirst());

            Console.WriteLine(list.RemoveLast());
            Console.WriteLine(list.RemoveLast());
        }
        public void ForEach_MultipleElements_ShouldEnumerateElementsCorrectly()
        {
            // Arrange
            var list = new DoublyLinkedList <string>();

            list.AddLast("Five");
            list.AddLast("Six");
            list.AddLast("Seven");

            // Act
            var items = new List <string>();

            list.ForEach(items.Add);

            // Assert
            Assert.Equal(items, new List <string> {
                "Five", "Six", "Seven"
            });
        }
示例#4
0
        static void Main(string[] args)
        {
            DoublyLinkedList <string> list = new DoublyLinkedList <string>();

            list.Add("Два");
            list.Add("Три");
            list.Add("Четыре");
            list.Add("Пять");
            list.AppendFirst("Раз");

            foreach (var s in list.GetEnumerator())
            {
                Console.Write(s + " ");
            }

            Console.WriteLine();

            foreach (var s in list.GetEnumerator(true))
            {
                Console.Write(s + " ");
            }

            Console.WriteLine();
            Console.WriteLine(list.Count);

            if (list.Contains("Три"))
            {
                list.Remove("Три");
            }

            if (!list.IsNull)
            {
                Console.WriteLine(list.Count);
            }

            foreach (var s in list.GetEnumerator())
            {
                Console.Write(s + " ");
            }

            Console.ReadKey();
        }
示例#5
0
        }//end Method

        /// <summary>
        ///  Insert Nodes at end on the list 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="newData"></param>
        public void InsertLast(DoublyLinkedList list, double newData)
        {
            // Create Node object
            Node newNode = new Node(newData);

            if (list.head == null)
            {
                newNode.previous = null;
                list.head = newNode;
                return;
            }// end if

            Node lastNode = GetLastNode(list);
            lastNode.next = newNode;
            newNode.previous = lastNode;




        }// end method
        public void RemoveFirst_OneElement_ShouldMakeListEmpty()
        {
            // Arrange
            var list = new DoublyLinkedList <int>();

            list.AddLast(5);

            // Act
            var element = list.RemoveFirst();

            // Assert
            Assert.Equal(5, element);
            Assert.Equal(0, list.Count);

            var items = new List <int>();

            list.ForEach(items.Add);
            Assert.Equal(items, new List <int> {
            });
        }
        public void AddLast_SeveralElements_ShouldAddElementsCorrectly()
        {
            // Arrange
            var list = new DoublyLinkedList <int>();

            // Act
            list.AddLast(5);
            list.AddLast(10);
            list.AddLast(15);

            // Assert
            Assert.Equal(3, list.Count);

            var items = new List <int>();

            list.ForEach(items.Add);
            Assert.Equal(items, new List <int> {
                5, 10, 15
            });
        }
示例#8
0
        private static void Main()
        {
            var doublyLinkedList = new DoublyLinkedList();

            doublyLinkedList.Add(5);
            doublyLinkedList.Add(10);
            doublyLinkedList.Add(15);
            Console.WriteLine(doublyLinkedList.Contains(10));
            Console.WriteLine(doublyLinkedList.Contains(25));
            Console.WriteLine(doublyLinkedList.Remove(10));
            Console.WriteLine(doublyLinkedList.FindByIndex(1));
            Console.WriteLine(doublyLinkedList.Count);
            Console.WriteLine(doublyLinkedList[1]);
            doublyLinkedList[1] = 20;
            Console.WriteLine(doublyLinkedList[1]);
            doublyLinkedList.Clear();
            doublyLinkedList.Add(1);
            doublyLinkedList.Add(3);
            Console.WriteLine("Insert after: " + doublyLinkedList.InsertBefore(1, 2));
            Console.Read();
        }
        static void Main(string[] args)
        {
            // DoublyLinkedList instanziieren
            var list = new DoublyLinkedList <string>();

            list.Add("Erster Wert");
            list.Add("Zweiter Wert");
            list.Add("Dritter Wert");
            list.Add("Vierter Wert");

            list.Remove("Dritter Wert");

            foreach (string name in list)
            {
                Console.WriteLine(name);
            }

            Console.WriteLine(list);

            Console.WriteLine("Weiter mit ENTER . . .");
            Console.ReadLine();
        }
示例#10
0
        }//end method

        /// <summary>
        /// // Get Number of Nodes
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int GetNumNodes(DoublyLinkedList list)
        {

            Node node = list.head;
            int result = 0;

            if (node != null)
            {

                result++;

                while (node.next != null)
                {
                    node = node.next;
                    result++;
                }//end loop

            }// end if

            return result;

        }//end method
        public void IEnumerable_Foreach_MultipleElements()
        {
            // Arrange
            var list = new DoublyLinkedList <string>();

            list.AddLast("Five");
            list.AddLast("Six");
            list.AddLast("Seven");

            // Act
            var items = new List <string>();

            foreach (var element in list)
            {
                items.Add(element);
            }

            // Assert
            Assert.Equal(items, new List <string> {
                "Five", "Six", "Seven"
            });
        }
        public void RemoveLast_SeveralElements_ShouldRemoveElementsCorrectly()
        {
            // Arrange
            var list = new DoublyLinkedList <int>();

            list.AddFirst(10);
            list.AddFirst(9);
            list.AddFirst(8);

            // Act
            var element = list.RemoveLast();

            // Assert
            Assert.Equal(10, element);
            Assert.Equal(2, list.Count);

            var items = new List <int>();

            list.ForEach(items.Add);
            Assert.Equal(items, new List <int> {
                8, 9
            });
        }
示例#13
0
        static void Main(string[] args)
        {
            //Create Linked list
            var dll = new DoublyLinkedList <int>(7);

            dll.Print();

            //Push 1 at the beginning of the list
            dll.Push(1);

            //Push 2 at the beginning of the list
            dll.Push(2);

            //Push 4 after Head.Next
            var four = dll.InsertAfter(dll.Head.Next, 4);

            //Push 5 before 1
            dll.InsertBefore(dll.Head.Next, 5);

            //Append 9
            var nine = dll.Append(9);

            //Deletes 2 and makes 1 Head
            dll.Delete(dll.Head);

            //Delete 9
            dll.Delete(nine);

            //Delete 4
            dll.Delete(four);

            Console.ReadLine();

            dll.Print();
            //Finish program
            Console.ReadLine();
        }
        public void IEnumerable_NonGeneric_MultipleElements()
        {
            // Arrange
            var list = new DoublyLinkedList <object>();

            list.AddLast("Five");
            list.AddLast(6);
            list.AddLast(7.77);

            // Act
            var enumerator = ((IEnumerable)list).GetEnumerator();
            var items      = new List <object>();

            while (enumerator.MoveNext())
            {
                items.Add(enumerator.Current);
            }

            // Assert
            Assert.Equal(items, new List <object>()
            {
                "Five", 6, 7.77
            });
        }
        static void Main(string[] args)
        {
            DoublyLinkedList <string> linkedList = new DoublyLinkedList <string>();

            // добавление элементов
            linkedList.Add("Bob");
            linkedList.Add("Bill");
            linkedList.Add("Tom");
            linkedList.AddFirst("Kate");
            foreach (var item in linkedList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("-----------------------");

            // удаление
            linkedList.Remove("Bill");

            // перебор с последнего элемента
            foreach (var t in linkedList.BackEnumerator())
            {
                Console.WriteLine(t);
            }
        }
示例#16
0
        static void Main(string[] args)
        {
            DoublyLinkedList dll = new DoublyLinkedList();

            //dll.insertAtBeginning(1);
            //dll.insertAtEnd(2);
            //dll.insertAtEnd(3);
            //dll.printList();
            //Console.WriteLine("\nLength is: " + dll.getLength());
            //dll.insertAtEnd(4);
            //dll.insertAtEnd(5);
            //dll.insertAtPosition(3, 10);
            //dll.printList();
            //Console.WriteLine("\nLength is: " + dll.getLength());
            //dll.reverseList();
            //dll.printList();
            //dll.head = new DoublyLinkedList.Node(10);
            dll.createList(8);
            //dll.generateTestList(8);
            dll.printList();
            dll.swapKthNode(3);
            dll.printList();
            Console.ReadKey();
        }
示例#17
0
 public Enumerator(DoublyLinkedList <T> list)
 {
     this.list = list;
     index     = -1;
 }
示例#18
0
        static void Main(string[] args)
        {
            int choice, data, k, x;
            DoublyLinkedList list = new DoublyLinkedList();

            list.CreateList();
            while (true)
            {
                Console.WriteLine("1.Display the list");
                Console.WriteLine("2.Insert a node in the beginning of the list");
                Console.WriteLine("3.Insert in empty list");
                Console.WriteLine("4.Insert a node at the end of the list");
                Console.WriteLine("5.Insert a node after a specified node");
                Console.WriteLine("6.Insert a node before a specified node");;
                Console.WriteLine("7.Delete first node");
                Console.WriteLine("8.Delete last node");
                Console.WriteLine("9.Delete any node");
                Console.WriteLine("10.Reverse the list");
                Console.WriteLine("11.Quit");
                Console.WriteLine("Enter your choice:");
                choice = Convert.ToInt32(Console.ReadLine());
                if (choice == 11)
                {
                    break;
                }
                switch (choice)
                {
                case 1:
                    list.DisplayList();
                    break;

                case 2:
                    data = Convert.ToInt32(Console.ReadLine());
                    list.InsertInBeginning(data);
                    break;

                case 3:
                    data = Convert.ToInt32(Console.ReadLine());
                    list.InsertInEmpty(data);
                    break;

                case 4:
                    data = Convert.ToInt32(Console.ReadLine());
                    list.InsertAtEnd(data);
                    break;

                case 5:
                    Console.WriteLine("Enter the element to be inserted:");
                    data = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter the element after which to inserted:");
                    x = Convert.ToInt32(Console.ReadLine());
                    list.InsertAfter(data, x);
                    break;

                case 6:
                    Console.WriteLine("Enter the element to be inserted:");
                    data = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter the element before which to inserted:");
                    x = Convert.ToInt32(Console.ReadLine());
                    list.InsertBefore(data, x);
                    break;
                }
            }
        }
示例#19
0
 internal LinkedListNode(DoublyLinkedList <T> list, T value)
 {
     this.list = list;
     this.item = value;
 }
示例#20
0
 internal void DumpNode()
 {
     list = null;
     next = null;
     prev = null;
 }
        }//end Constructor

        private void btnStart_Click(object sender, EventArgs e)
        {
            lstOutput.Items.Clear();

            #region Linked List Created
            //Random Object
            Random rand = new Random();
            int    size = 3;

            //list object
            DoublyLinkedList list = new DoublyLinkedList();

            //iterate list from last
            for (int i = 0; i < size; i++)
            {
                list.InsertLast(list, Math.Round(RandomDouble(rand, 0, 100.00), 2));
            }//end loop

            int numNodes = list.GetNumNodes(list);
            Console.WriteLine("Num Nodes is " + numNodes + ".");
            #endregion

            #region Print Linked List

            // Print Forward
            lstOutput.Items.Add(string.Format("{0}", "Nodes Printed Forwards:"));
            lstOutput.Items.Add(string.Format("{0}", "-----------------------------"));

            Node node  = list.GetFirstNode(list);
            int  index = 0;

            // Next Node
            while (node != null)
            {
                // Display Output
                lstOutput.Items.Add(string.Format("Node {0,2}:    {1:00.00}", index++, node.data));
                node = node.next;
            }//end loop

            lstOutput.Items.Add("");


            // Call node object
            node  = list.GetLastNode(list);
            index = 0;

            // Print Backwards
            lstOutput.Items.Add(string.Format("{0}", "Nodes Printed Backwards:"));
            lstOutput.Items.Add(string.Format("{0}", "-----------------------------"));

            // Previous Node
            while (node != null)
            {
                // Display Output
                lstOutput.Items.Add(string.Format("Node {0,2}:    {1:00.00}", index++, node.data));
                node = node.previous;
            }//end loop

            #endregion

            // Print Backwards Plus 10
            lstOutput.Items.Add("");

            for (int i = 0; i < 10; i++)
            {
                list.InsertAfter(list.head.next, Math.Round(RandomDouble(rand, 0, 100.00), 2));
            }//end loop

            // Call node object
            node  = list.GetLastNode(list);
            index = 0;

            // Print Backwards + 10
            lstOutput.Items.Add(string.Format("{0}", "Nodes Printed Backwards + 10:"));
            lstOutput.Items.Add(string.Format("{0}", "-----------------------------"));

            while (node != null)
            {
                lstOutput.Items.Add(string.Format("Node {0,2}:    {1:00.00}", index++, node.data));
                node = node.previous;
            } //end loop
        }     // end method
示例#22
0
        static void Main(string[] args)
        {
            var       x     = new DoublyLinkedList();
            Stopwatch timer = new Stopwatch();

            //for (int y = 0; y < 1; y++ )
            //{
            //    x.AddToHead(new Node(y));
            //}

            //timer.Start();

            //for (int y = 0; y < 1; y++ )
            //{
            //    x.Delete(y);
            //}
            //timer.Stop();
            //Console.WriteLine(timer.Elapsed);
            //timer.Reset();

            //for (int y = 0; y < 3; y++)
            //{
            //    x.AddToHead(new Node(y));
            //}

            //timer.Start();

            //for (int y = 0; y < 3; y++)
            //{
            //    x.Delete(y);
            //}
            //timer.Stop();
            //Console.WriteLine(timer.Elapsed);
            //timer.Reset();

            //for (int y = 0; y < 5; y++)
            //{
            //    x.AddToHead(new Node(y));
            //}

            //timer.Start();

            //for (int y = 0; y < 5; y++)
            //{
            //    x.Delete(y);
            //}
            //timer.Stop();
            //Console.WriteLine(timer.Elapsed);
            //timer.Reset();

            //for (int y = 0; y < 10; y++)
            //{
            //    x.AddToHead(new Node(y));
            //}

            //timer.Start();

            //for (int y = 0; y < 10; y++)
            //{
            //    x.Delete(y);
            //}
            //timer.Stop();
            //Console.WriteLine(timer.Elapsed);
            //timer.Reset();

            //for (int y = 0; y < 100; y++)
            //{
            //    x.AddToHead(new Node(y));
            //}

            //timer.Start();

            //for (int y = 0; y < 100; y++)
            //{
            //    x.Delete(y);
            //}
            //timer.Stop();
            //Console.WriteLine(timer.Elapsed);
            //timer.Reset();

            //for (int y = 0; y < 1000; y++)
            //{
            //    x.AddToHead(new Node(y));
            //}

            //timer.Start();

            //for (int y = 0; y < 1000; y++)
            //{
            //    x.Delete(y);
            //}
            //timer.Stop();
            //Console.WriteLine(timer.Elapsed);
            //timer.Reset();

            for (int y = 0; y < 1; y++)
            {
                x.AddToHead(new Node(y));
            }

            timer.Start();

            for (int y = 0; y < 1; y++)
            {
                x.DeleteFromTail();
            }

            timer.Stop();
            Console.WriteLine(timer.Elapsed);
            timer.Reset();

            for (int y = 0; y < 3; y++)
            {
                x.AddToHead(new Node(y));
            }

            timer.Start();

            for (int y = 0; y < 3; y++)
            {
                x.DeleteFromTail();
            }

            timer.Stop();
            Console.WriteLine(timer.Elapsed);
            timer.Reset();

            for (int y = 0; y < 5; y++)
            {
                x.AddToHead(new Node(y));
            }

            timer.Start();

            for (int y = 0; y < 5; y++)
            {
                x.DeleteFromTail();
            }

            timer.Stop();
            Console.WriteLine(timer.Elapsed);
            timer.Reset();

            for (int y = 0; y < 10; y++)
            {
                x.AddToHead(new Node(y));
            }

            timer.Start();

            for (int y = 0; y < 10; y++)
            {
                x.DeleteFromTail();
            }

            timer.Stop();
            Console.WriteLine(timer.Elapsed);
            timer.Reset();

            for (int y = 0; y < 100; y++)
            {
                x.AddToHead(new Node(y));
            }

            timer.Start();

            for (int y = 0; y < 100; y++)
            {
                x.DeleteFromTail();
            }

            timer.Stop();
            Console.WriteLine(timer.Elapsed);
            timer.Reset();

            for (int y = 0; y < 1000; y++)
            {
                x.AddToHead(new Node(y));
            }

            timer.Start();

            for (int y = 0; y < 1000; y++)
            {
                x.DeleteFromTail();
            }

            timer.Stop();
            Console.WriteLine(timer.Elapsed);
            timer.Reset();

            Console.ReadLine();
        }
示例#23
0
 public void Dispose()
 {
     list  = null;
     index = -1;
 }
 static void Main(string[] args)
 {
     DoublyLinkedList test = new DoublyLinkedList();
     test.InsertAt(0, 1);
     test.InsertAt(1, 2);
     test.Add(3);
     Console.WriteLine("List elements:");
     test.Print();
     test.RemoveAt(0);
     Console.WriteLine("List elements after removing element at index 0:");
     test.Print();
     test.InsertAt(2, 4);
     Console.WriteLine("List elements after inserting an element at index 2(after the last element):");
     test.Print();
     Console.WriteLine("List elements after changing element data at index 1 with '5':");
     test.ChangeDataAtIndex(1, 5);
     test.Print();
     Console.WriteLine("List elements after changing element with value '2' to '3':");
     test.ChangeElementData(2, 3);
     test.Print();
     Console.WriteLine("List elements after removing element with value '4'");
     test.Remove(4);
     test.Print();
     object element = test.GetElementData(1);
     Console.WriteLine("Value of element at index 1:" + element);
     Console.WriteLine("Index of element with value 3:{0}", test.IndexOf(3));
     Console.WriteLine("List contains element with value = '5': {0}", (test.Contains(5)) ? "Yes" : "No");
     Console.WriteLine("Is list empty? - {0}", (test.IsEmpty) ? "Yes" : "No");
 }