Exemplo n.º 1
0
        public static void Main()
        {
            DoublyLinkedList <int> doublyLinkedList = new DoublyLinkedList <int>();

            doublyLinkedList.Add(1);
            doublyLinkedList.Add(2);
            doublyLinkedList.Add(3);
            doublyLinkedList.Add(4);
            doublyLinkedList.Add(5);
            doublyLinkedList.InsertAfter(5, 99);

            doublyLinkedList.Delete(5);

            Console.WriteLine(doublyLinkedList.Contains(5));

            foreach (object item in doublyLinkedList)
            {
                Console.WriteLine(item);
            }

            foreach (object item in doublyLinkedList.Reverse())
            {
                Console.WriteLine(item);
            }

            CircularDoublyLinkedList <int> circularDoublyLinkedList = new CircularDoublyLinkedList <int>();

            circularDoublyLinkedList.Add(1);
            circularDoublyLinkedList.Add(2);
            circularDoublyLinkedList.Add(3);
            circularDoublyLinkedList.Add(4);
            circularDoublyLinkedList.Add(5);

            circularDoublyLinkedList.Delete(3);

            circularDoublyLinkedList.InsertAfter(2, 99);

            Console.WriteLine(circularDoublyLinkedList.Contains(3));

            foreach (object item in circularDoublyLinkedList)
            {
                Console.WriteLine(item);
            }
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
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;
                }
            }
        }
        }//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