static void Main(string[] args) { Console.WriteLine("Singly Linked List"); SinglyLinkedList myList1 = new SinglyLinkedList(); Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(4); myList1.AddToFront(n1); myList1.AddToEnd(n2); myList1.AddToFront(n3); myList1.AddToEnd(n4); myList1.Print(); myList1.RemoveNode(myList1.Head, n2.Value); myList1.Print(); myList1.RemoveFirst(); myList1.Print(); myList1.RemoveLast(); myList1.Print(); myList1.RemoveLast(); myList1.Print(); Console.WriteLine("Doubly Linked List"); DoublyLinkedList myList2 = new DoublyLinkedList(); myList2.AddToFront(n1); myList2.AddToEnd(n2); myList2.AddToFront(n3); myList2.AddToEnd(n4); myList2.Print(); myList2.RemoveNode(myList2.Head, n2.Value); myList2.Print(); myList2.RemoveFirst(); myList2.Print(); myList2.RemoveLast(); myList2.Print(); myList2.RemoveLast(); myList2.Print(); myList2.RemoveFirst(); myList2.Print(); //Post Fix Calculator string[] expression = new string[] { "5", "6", "7", "*", "+", "1", "-" }; Console.WriteLine("Postfix calculator"); PostfixCalculator.Print(expression); Console.WriteLine("Result: " + PostfixCalculator.Calculate(expression)); }
public void AddRemoveToGetToStateWithSingleNode_CorrectState() { _list.AddFirst(1); _list.AddFirst(2); _list.RemoveFirst(); CheckStateWithSingleNode(_list); _list.AddFirst(2); _list.RemoveLast(); CheckStateWithSingleNode(_list); }
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) }