public static void Main() { SinglyLinkedList <int> ll = new SinglyLinkedList <int>(); Console.WriteLine("Populate new linked list using AddFirst, AddLast and Insert methods."); ll.AddFirst(3); ll.Print(); // Create new node to insert before. Node <int> n = new Node <int>(7); // AddLast using node. ll.AddLast(n); ll.Print(); // Insert 5 before the 7 node. ll.InsertBefore(n, 5); ll.Print(); // AddLast by value. ll.AddLast(9); ll.Print(); Console.WriteLine("Remove nodes using RemoveFirst, RemoveLast, Remove(value) methods."); ll.Remove(7); ll.Print(); ll.RemoveFirst(); ll.Print(); ll.RemoveLast(); ll.Print(); ll.Clear(); Console.WriteLine("Clear linked list."); Console.WriteLine($"Linked list is Empty: {ll.IsEmpty()}"); }
public static void Main() { // Declare new linked list of type string, using Generics<T>. // Big Oh notated below in Main. SinglyLinkedList <string> ll = new SinglyLinkedList <string>(); Console.WriteLine("Populate new linked list using AddFirst, AddLast and Insert methods."); // O(1) ll.AddFirst("Three"); ll.Print(); // Create new node to insert before, O(1). Node <string> n = new Node <string>("Seven"); // AddLast by node, O(n). ll.AddLast(n); ll.Print(); // Insert 5 before the 7 node, O(n). ll.InsertBefore(n, "Five"); ll.Print(); // AddLast by value, O(n). ll.AddLast("Nine"); ll.Print(); Console.WriteLine("Remove nodes using RemoveFirst, RemoveLast, Remove(value) methods."); ll.Remove("Seven"); // O(n) ll.Print(); // O(n) ll.RemoveFirst(); // O(1) ll.Print(); ll.RemoveLast(); // O(n) ll.Print(); ll.Clear(); // O(1) Console.WriteLine("Clear linked list."); Console.WriteLine($"Linked list is Empty: {ll.IsEmpty()}"); // O(1) }