Exemplo n.º 1
0
        private static DoublyLinkedList GetDoublyLinkedlistFromInput()
        {
            DoublyLinkedList doublyLinkedList = null;

            Console.WriteLine("Enter the number of elements in the " +
                              "doubly linked list");
            try
            {
                int numberElements = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter the elements separated by space, comma" +
                                  " or semi-colon");
                String[] elements = Console.ReadLine().Split(' ', ',', ';');
                doublyLinkedList = new DoublyLinkedList(null);
                DoublyLinkedListNode node = null;
                for (int index = 0; index < numberElements; index++)
                {
                    node = doublyLinkedList.InsertBefore(node,
                                                         int.Parse(elements[index]));
                }
                doublyLinkedList.SetDoublyLinkedListHead(node);
            }
            catch (Exception exception) {
                Console.WriteLine("Thrown exception is " + exception.Message);
            }

            return(doublyLinkedList);
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
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.º 4
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;
                }
            }
        }