Beispiel #1
0
        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 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()}");
        }
Beispiel #3
0
        static void LinkedListInsert()
        {
            SinglyLinkedList <int> linkedList = new SinglyLinkedList <int>(0);

            linkedList.Append(1);
            linkedList.Append(2);
            linkedList.Append(3);
            linkedList.Append(4);
            linkedList.Append(5);
            linkedList.Append(6);
            linkedList.Print();

            linkedList.Insert(65656, 4);
            linkedList.RemoveAtIndex(4);
            linkedList.RemoveAtIndex(0);

            linkedList.Print();

            linkedList.Reverse();
            linkedList.Print();
        }
Beispiel #4
0
        static void LinkedListPrepend()
        {
            SinglyLinkedList <int> linkedList = new SinglyLinkedList <int>(0);

            linkedList.Prepend(1);
            linkedList.Prepend(2);
            linkedList.Prepend(3);
            linkedList.Prepend(4);
            linkedList.Prepend(5);
            linkedList.Prepend(6);
            linkedList.Print();
        }
Beispiel #5
0
        public static void Main()
        {
            // Create a new singly linked list of type string.
            SinglyLinkedList <string> sll = new SinglyLinkedList <string>();

            Console.WriteLine("Populate new singly linked list of strings with duplicates...");
            sll.AddFirst("Dog");
            sll.AddLast("Cat");
            sll.AddLast("Pig");
            sll.AddLast("Cat");
            sll.AddLast("Dog");
            sll.Print();

            // Remove Duplicates with no memory runs in O(n^2) time, with O(1) additional space.
            Console.WriteLine();
            Console.WriteLine("Remove Dupes using no additional memory:");
            sll.RemoveDupesNoMemory(sll.Head);
            sll.Print();

            Console.WriteLine();
            Console.WriteLine("Clear linked list.");
            sll.Clear();
            Console.WriteLine($"Linked list is Empty: {sll.IsEmpty()}");
            Console.WriteLine();


            Console.WriteLine("Populate new singly linked list of strings with duplicates...");
            sll.AddFirst("Jack");
            sll.AddLast("Jill");
            sll.AddLast("John");
            sll.AddLast("Jack");
            sll.AddLast("Jill");
            sll.Print();

            // Remove dupes using HashSet runs in O(n) time, with O(n) added memory.
            Console.WriteLine();
            Console.WriteLine("Remove Dupes using additional memory (HashSet):");
            sll.RemoveDupesWithMemory(sll.Head);
            sll.Print();
        }
Beispiel #6
0
        public static void Main()
        {
            // Create a new singly linked list of type string.
            SinglyLinkedList <string> sll = new SinglyLinkedList <string>();

            Console.WriteLine("Populate new singly linked list that is NOT a palindrome...");
            sll.AddFirst("Dog");
            sll.AddLast("Cat");
            sll.AddLast("Pig");
            sll.AddLast("Farm");
            sll.Print();

            // The IsPalindrome contains 3 methods:
            // IsPalindrome: Call two helper methods: O(2n) (including helper method execution)
            // ReverseAndCopy: O(n), with O(n) space.
            // IsEqual: O(n)
            // Runtime for IsPalindrome method: O(2n) -> O(n)
            // with O(n) extra memory.
            // Same can be done using a Stack...instead of copying a new linked list.

            Console.WriteLine($"Is linked list a palindrome: {sll.IsPalindrome(sll.Head)}");

            Console.WriteLine();
            Console.WriteLine("Clear linked list.");
            sll.Clear();
            Console.WriteLine($"Linked list is Empty: {sll.IsEmpty()}");
            Console.WriteLine();

            Console.WriteLine("Populate new singly linked list that IS a palindrome...");
            sll.AddFirst("Dog");
            sll.AddLast("Cat");
            sll.AddLast("Pig");
            sll.AddLast("Cat");
            sll.AddLast("Dog");
            sll.Print();

            Console.WriteLine($"Is linked list a palindrome: {sll.IsPalindrome(sll.Head)}");
        }
Beispiel #7
0
        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)
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            #region " Singly Linked List "

            Console.WriteLine("============= Singly Linked List =============");
            SinglyLinkedList <int> ls = new SinglyLinkedList <int>();
            ls.Push(0);
            ls.Push(1);
            ls.Push(2);
            ls.Push(3);
            Console.Write("Singly linked List: ");
            ls.Print();
            Console.Write("Singly linked List Reversed: ");
            ls.Reverse();
            ls.Print();
            Console.WriteLine($"Popped Value: {ls.Pop().Value}");
            Console.WriteLine($"Popped Value: {ls.Pop().Value}");
            Console.WriteLine($"Popped Value: {ls.Pop().Value}");
            Console.WriteLine($"Popped Value: {ls.Pop().Value}");
            ls.Print();

            #endregion

            #region " Stack "

            Console.WriteLine("============= Stack =============");
            Stack <int> stack = new Stack <int>();
            stack.Push(1);
            stack.Push(2);
            stack.Push(3);
            stack.Push(4);

            Console.WriteLine($"Number of elements in Stack is {stack.Count}");
            Console.WriteLine(stack.Pop().Value);
            Console.WriteLine(stack.Pop().Value);
            Console.WriteLine(stack.Pop().Value);
            Console.WriteLine(stack.Pop().Value);
            Console.WriteLine($"Number of elements in Stack is {stack.Count}");

            #endregion

            #region " Queue "

            Console.WriteLine("============= Queue =============");
            Queue <int> data = new Queue <int>();
            data.Enqueue(1);
            data.Enqueue(2);
            data.Enqueue(3);
            data.Enqueue(4);

            Console.WriteLine($"Number of elements in Queue is {data.Count}");
            Console.WriteLine(data.Dequeue().Value);
            Console.WriteLine(data.Dequeue().Value);
            Console.WriteLine(data.Dequeue().Value);
            Console.WriteLine(data.Dequeue().Value);
            Console.WriteLine($"Number of elements in Queue is {data.Count}");

            #endregion

            #region " Binary Search Tree "

            Console.WriteLine("============= Binary Search Tree =============");

            /*
             *                      10
             *                     /  \
             *                    6   15
             *                   / \  / \
             *                  3  8 11  20
             *                 /
             *                0
             */
            BinarySearchTree myBST = new BinarySearchTree();
            myBST.AddToTree(10);
            myBST.AddToTree(6);
            myBST.AddToTree(15);
            myBST.AddToTree(3);
            myBST.AddToTree(8);
            myBST.AddToTree(11);
            myBST.AddToTree(20);
            myBST.AddToTree(0);
            Console.WriteLine($"BFS Traversal Data: ");
            PrintList(myBST.BFS()); // 10,6,15,3,8,11,20,0

            Console.WriteLine($"DFS-PreOrder Traversal Data: ");
            PrintList(myBST.DFS_PreOrder_Iterative()); // 10,6,3,0,8,15,11,20
            PrintList(myBST.DFS_PreOrder_Recursive());

            Console.WriteLine($"DFS-InOrder Traversal Data: ");
            PrintList(myBST.DFS_InOrder_Iterative()); // 0,3,6,8,10,11,15,20
            PrintList(myBST.DFS_InOrder_Recursive());

            Console.WriteLine($"DFS-PostOrder Traversal Data: ");
            PrintList(myBST.DFS_PostOrder_Iterative()); // 0,3,8,6,11,20,15,10
            PrintList(myBST.DFS_PostOrder_Recursive());
            #endregion

            #region " Max Binary Heap "

            Console.WriteLine("============= Max Binary Heap =============");
            MaxBinaryHeap maxHeap = new MaxBinaryHeap();
            maxHeap.Insert(18);
            maxHeap.Insert(27);
            maxHeap.Insert(12);
            maxHeap.Insert(39);
            maxHeap.Insert(33);
            maxHeap.Insert(41);
            PrintList(maxHeap.Values);
            Console.WriteLine(maxHeap.ExtractMax());
            Console.WriteLine(maxHeap.ExtractMax());
            Console.WriteLine(maxHeap.ExtractMax());
            Console.WriteLine(maxHeap.ExtractMax());
            Console.WriteLine(maxHeap.ExtractMax());
            Console.WriteLine(maxHeap.ExtractMax());

            #endregion

            #region " Min Binary Heap "

            Console.WriteLine("============= Min Binary Heap =============");
            MinBinaryHeap minHeap = new MinBinaryHeap();
            minHeap.Insert(18);
            minHeap.Insert(27);
            minHeap.Insert(12);
            minHeap.Insert(39);
            minHeap.Insert(33);
            minHeap.Insert(41);
            PrintList(minHeap.Values);
            Console.WriteLine(minHeap.ExtractMin());
            Console.WriteLine(minHeap.ExtractMin());
            Console.WriteLine(minHeap.ExtractMin());
            Console.WriteLine(minHeap.ExtractMin());
            Console.WriteLine(minHeap.ExtractMin());
            Console.WriteLine(minHeap.ExtractMin());

            #endregion

            #region " Priority Queue "

            Console.WriteLine("============= Priority Queue =============");

            PriorityQueue queue = new PriorityQueue();
            queue.Enqueue("common cold", 1);
            queue.Enqueue("knife stab", 5);
            queue.Enqueue("high fever", 3);
            queue.Enqueue("dislocated arm", 4);
            queue.Enqueue("flu", 2);
            Console.WriteLine(queue.Dequeue().Value);
            Console.WriteLine(queue.Dequeue().Value);
            Console.WriteLine(queue.Dequeue().Value);
            Console.WriteLine(queue.Dequeue().Value);
            Console.WriteLine(queue.Dequeue().Value);

            #endregion

            #region  " Graphs "

            Console.WriteLine("============= Graphs =============");
            // Undirected Graph

            /**
             *                  Chicago
             *                  |     \
             *                  |      \
             *              Dallas    Atlanta
             *                  |         \
             *                  |          \
             *           San Francisco----Orlando
             *                   \        /
             *                    \     /
             *                   Las Vegas
             * **/
            var udGraph = new Graph();
            udGraph.AddVertex("Chicago");
            udGraph.AddVertex("Dallas");
            udGraph.AddVertex("Atlanta");
            udGraph.AddVertex("San Francisco");
            udGraph.AddVertex("Orlando");
            udGraph.AddVertex("Las Vegas");

            udGraph.AddAnEdge("Chicago", "Dallas");
            udGraph.AddAnEdge("Chicago", "Atlanta");
            udGraph.AddAnEdge("Dallas", "San Francisco");
            udGraph.AddAnEdge("Atlanta", "Orlando");
            udGraph.AddAnEdge("San Francisco", "Orlando");
            udGraph.AddAnEdge("San Francisco", "Las Vegas");
            udGraph.AddAnEdge("Orlando", "Las Vegas");

            PrintList(udGraph.DFSRecursive("Chicago"));
            PrintList(udGraph.DFSIterative("Chicago"));
            PrintList(udGraph.BFSTraversal("Chicago"));

            #endregion

            #region  " Topological Sort "
            Console.WriteLine("============= Topological Sort =============");

            TopologicalSort tSorter = new TopologicalSort();
            var             adjList = new System.Collections.Generic.Dictionary <string, string[]>();
            adjList.Add("A", new string[] { "B", "C" });
            adjList.Add("B", new string[] { "D", "C" });
            adjList.Add("C", new string[] { "E" });
            adjList.Add("D", new string[] { "E", "F" });
            adjList.Add("G", new string[] { "E", "F" });

            bool hasCycle = tSorter.IsThereALoop(adjList);
            Console.WriteLine($"Given Graph has a cycle? {hasCycle}");
            if (!hasCycle)
            {
                Console.WriteLine($"Topological Order for the give graph: ");
                PrintList(tSorter.FindTopologicalSort(adjList));
            }

            adjList = new System.Collections.Generic.Dictionary <string, string[]>();
            adjList.Add("A", new string[] { "B", "C" });
            adjList.Add("B", new string[] { "D", "C" });
            adjList.Add("C", new string[] { "E", "A" });
            adjList.Add("D", new string[] { "E", "F" });
            adjList.Add("G", new string[] { "E", "F" });

            hasCycle = tSorter.IsThereALoop(adjList);
            Console.WriteLine($"Given Graph has a cycle? {hasCycle}");
            if (!hasCycle)
            {
                Console.WriteLine($"Topological Order for the give graph: ");
                PrintList(tSorter.FindTopologicalSort(adjList));
            }

            #endregion

            Console.ReadLine();
        }
Beispiel #9
0
 public void Print()
 {
     list.Reverse();
     list.Print();
     list.Reverse();
 }