static void Main(string[] args) { DoublyLinkedList myList = new DoublyLinkedList(); myList.AddToEnd(9); myList.AddToEnd(17); myList.AddToEnd(40); myList.AddToEnd(7); //myList.AddToBeginning(7); myList.Print(); Console.WriteLine("\n"); //myList.Find(17); //myList.Find(10); //myList.AddAfter(39, 17); //myList.Print(); //myList.Length(); //myList.Remove(9); ////myList.AddBefore(13, 40); //myList.Print(); }
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; } } }
static void Main(string[] args) { WriteLine("Doubly Linked List Implentation"); // Searching - // O(n) time - Just searching for a node by traversing the entire linked list | // O(1) space - Not storing anything other than current Node, because pointer points to different Node // Removing One Item // O(1) time | O(1) space // Removing Two or More Item // O(n) time | O(1) time // SetHead(), SetTail(), insertBefore(), insertAfter() // O(1) time | O(1) space // insertAtPosition(), // O(p) time -- Because we have to traverse all the way to the position | // O(1) space WriteLine(); DoublyLinkedList dllist = new DoublyLinkedList(); dllist.SetHead(500); // dllist.SetHead(200); dllist.SetTail(700); dllist.SetTail(50); dllist.SetHead(600); dllist.PrintList(); WriteLine(); WriteLine($"Removing Head: {dllist.RemoveHeadOrFirst()}"); WriteLine(); dllist.PrintList(); WriteLine(); WriteLine($"Removing Tail: {dllist.RemoveTailOrLast()}"); WriteLine(); dllist.PrintList(); WriteLine(); WriteLine($"Removing At Position: {dllist.RemoveNodeAtPosition()}"); WriteLine(); dllist.PrintList(); WriteLine(); // WriteLine($"Removing Tail: {dllist.RemoveTailOrLast()}"); // WriteLine(); dllist.PrintList(); WriteLine(); WriteLine(); }