Example #1
0
        static void Main(string[] args)
        {
            DoublyLinkedList D = new DoublyLinkedList();

            int  choice, key;
            Node match;

            while (true)
            {
                Console.WriteLine("Enter your choice: \n1. Print all nodes\n2. Search Through Linked List\n3. Insert at front\n4. Insert at last\n5. Delete\n6. EXIT\n");
                choice = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("\n");

                if (choice == 1)
                {
                    D.PrintAllNodes();
                }
                else if (choice == 2)
                {
                    Console.WriteLine("Enter the data you want to search: ");
                    key = Convert.ToInt32(Console.ReadLine());

                    match = D.ListSearch(key);
                    if (match == null)
                    {
                        Console.WriteLine("No match found.");
                    }
                    else
                    {
                        Console.WriteLine("Macth successful!");
                        if (match.next == null)
                        {
                            Console.WriteLine("Next: " + "NULL");
                        }
                        else
                        {
                            Console.WriteLine("Next: " + match.next.key);
                        }
                        if (match.prev == null)
                        {
                            Console.WriteLine("Previous: " + "NULL");
                        }
                        else
                        {
                            Console.WriteLine("Previous: " + match.prev.key);
                        }
                        Console.WriteLine("Data: " + match.key);
                    }
                }
                else if (choice == 3)
                {
                    Console.WriteLine("Enter the data you want to add: ");
                    key = Convert.ToInt32(Console.ReadLine());

                    D.InsertFirst(key);
                }
                else if (choice == 4)
                {
                    Console.WriteLine("Enter the data you want to add: ");
                    key = Convert.ToInt32(Console.ReadLine());

                    D.InsertLast(key);
                }
                else if (choice == 5)
                {
                    Console.WriteLine("Enter the element you want to delete: ");
                    key = Convert.ToInt32(Console.ReadLine());

                    D.ListDelete(key);
                }
                else
                {
                    break;
                }
            }
        }