Example #1
0
        public static void Main()
        {
            // Create a new doubly linked list of type int.
            DoublyLinkedList <int> dll = new DoublyLinkedList <int>();

            Console.WriteLine("Populate new doubly linked list that is NOT a palindrome...");
            dll.AddFirst(3);
            dll.AddLast(5);
            dll.AddLast(7);
            dll.AddLast(9);
            Console.WriteLine("Print:");
            dll.Print();
            Console.WriteLine("Print Reverse:");
            dll.PrintReverse();

            // The IsPalindrome contains 3 methods:
            // IsPalindrome: O(2n)
            // ReverseAndCopy: O(n), with O(n) space.
            // IsEqual: O(n)
            // Runtime for IsPalindrome method execution: O(2n) -> O(n)
            // with O(n) extra memory.
            // Still trying to work in generics, only accepts type int right now.

            Console.WriteLine($"Is linked list a palindrome: {dll.IsPalindrome(dll.Head)}");
            Console.WriteLine();
            Console.WriteLine("Clear linked list.");
            dll.Clear();
            Console.WriteLine($"Linked list is Empty: {dll.IsEmpty()}");
            Console.WriteLine();
            Console.WriteLine("Populate new doubly linked list that IS a palindrome...");
            dll.AddFirst(3);
            dll.AddLast(5);
            dll.AddLast(7);
            dll.AddLast(5);
            dll.AddLast(3);
            Console.WriteLine("Print:");
            dll.Print();
            Console.WriteLine("Print Reverse:");
            dll.PrintReverse();

            Console.WriteLine($"Is linked list a palindrome: {dll.IsPalindrome(dll.Head)}");
        }
Example #2
0
        private void shrink()
        {
            if (Math.Abs(filledBuckets - bucketSize * 0.3) < tolerance && bucketSize / 2 > initialBucketSize)
            {
                filledBuckets = 0;
                //reduce array by half
                var newBucketSize = bucketSize / 2;

                var smallerArray = new DoublyLinkedList <HashSetNode <T> > [newBucketSize];

                for (int i = 0; i < bucketSize; i++)
                {
                    var item = hashArray[i];

                    //hashcode changes when bucket size changes
                    if (item?.Head != null)
                    {
                        var current = item.Head;

                        //find new location for each item
                        while (current != null)
                        {
                            var next = current.Next;

                            var newIndex = Math.Abs(current.Data.Value.GetHashCode()) % newBucketSize;

                            if (smallerArray[newIndex] == null)
                            {
                                filledBuckets++;
                                smallerArray[newIndex] = new DoublyLinkedList <HashSetNode <T> >();
                            }

                            smallerArray[newIndex].InsertFirst(current);

                            current = next;
                        }
                    }
                }

                hashArray = smallerArray;
            }
        }
Example #3
0
        public static void Main()
        {
            // Declare new doubly linked list of type int, using Generics<T>.
            // Big Oh notated below in Main.
            DoublyLinkedList <int> ll = new DoublyLinkedList <int>();

            Console.WriteLine("Populate new doubly linked list of type int.");

            // AddFirst: O(1)
            ll.AddFirst(3);

            // Create new node to insert.
            var n = new Node <int>(7);

            // AddLast by node, O(1).
            ll.AddLast(n);

            // Insert 5 before the 7 node, O(n).
            ll.AddBefore(n, 5);
            // AddLast by value, O(1).
            ll.AddLast(9);
            Console.WriteLine("Print:");
            ll.Print(); // O(n)
            Console.WriteLine("Print Reverse:");
            // Print Reverse: O(n)
            ll.PrintReverse();
            Console.WriteLine("Remove nodes using RemoveFirst, RemoveLast, Remove(value) methods.");
            ll.Remove(7);                                               // Remove by value (search): O(n)
            ll.Print();
            ll.RemoveFirst();                                           // O(1)
            ll.Print();
            ll.RemoveLast();                                            // O(1)
            ll.Print();
            ll.Clear();                                                 // O(1)
            Console.WriteLine("Clear linked list.");
            Console.WriteLine($"Linked list is Empty: {ll.IsEmpty()}"); // O(1)

            // The Upshot: Doubly Linked Lists improve all operations at Tail from O(n) to O(1).
            // Still O(n) to search or print, methods requiring complete or partial iteration.
            // I used a Count variable, unlike the single list, because they improve the code.
        }
Example #4
0
        static void Main(string[] args)
        {
            DoublyLinkedList list = new DoublyLinkedList();

            list.AddFirst("1");
            list.AddFirst("2");
            list.AddFirst("3");
            list.AddLast("4");
            list.AddLast("5");
            list.AddFirst("6");
            list.DisplayList(); // 6 3 2 1 4 5

            Console.WriteLine();
            Console.WriteLine(list.GetAndRemoveFirst());
            list.DisplayList(); // 3 2 1 4 5

            Console.WriteLine();
            Console.WriteLine(list.GetAndRemoveLast());
            list.DisplayList(); // 3 2 1 4

            var findEl = list[2];

            Console.WriteLine();
            list.InsertAfter(findEl, "7");
            list.DisplayList(); // 3 2 1 7 4

            findEl = list[1];
            Console.WriteLine();
            list.RemoveElement(findEl);
            list.DisplayList(); // 3 1 7 4

            Console.WriteLine();
            list.AddFirst("8");
            list.AddLast("9");

            Console.ReadLine();
        }
Example #5
0
        public static void Main()
        {
            // Declare new doubly linked list of type int, using Generics<T>.
            // Big Oh notated below in Main.
            DoublyLinkedList <int> ll = new DoublyLinkedList <int>();

            Console.WriteLine("Populate new linked list using AddFirst, AddLast and AddBefore.");
            // O(1)
            ll.AddFirst(3);

            // Create new node to insert.
            var n = new Node <int>(7);

            // AddLast by node, O(1).
            ll.AddLast(n);

            // Insert 5 before the 7 node, O(n).
            ll.AddBefore(n, 5);
            // AddLast by value, O(1).
            ll.AddLast(9);
            Console.WriteLine("Print:");
            ll.Print(); // O(n)
            Console.WriteLine("Print Reverse:");
            // Print Reverse
            ll.PrintReverse();                                          // O(n)
            Console.WriteLine("Remove nodes using RemoveFirst, RemoveLast, Remove(value) methods.");
            ll.Remove(7);                                               // O(n)
            ll.Print();
            ll.RemoveFirst();                                           // O(1)
            ll.Print();
            ll.RemoveLast();                                            // O(1)
            ll.Print();
            ll.Clear();                                                 // O(1)
            Console.WriteLine("Clear linked list.");
            Console.WriteLine($"Linked list is Empty: {ll.IsEmpty()}"); // O(1)
        }
Example #6
0
 public Queue()
 {
     List = new DoublyLinkedList <T>();
 }
Example #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //Single Linked List
            Console.WriteLine("---Singly Linked List---");
            SinglyLinkedList mySingly = new SinglyLinkedList();

            mySingly.printList();
            mySingly.addNode(5);
            mySingly.addNode(69);
            mySingly.addNode(420);
            mySingly.addNode(666);
            mySingly.printList();
            mySingly.deleteNode(666);
            mySingly.deleteNode(7);
            mySingly.deleteNode(420);
            mySingly.printList();

            //Doubly Linked List

            Console.WriteLine("---Doubly Linked List---");
            DoublyLinkedList myDoubly = new DoublyLinkedList();

            myDoubly.addNode(9);
            myDoubly.addNode(10);
            myDoubly.addNode(69);
            myDoubly.addNode(420);
            myDoubly.addNode(666);
            myDoubly.printForward();
            myDoubly.printReverse();
            myDoubly.deleteNode(9);
            myDoubly.deleteNode(69);
            myDoubly.printForward();
            myDoubly.printReverse();

            //Binary Search Tree

            Console.WriteLine("---Binary Search Tree---");
            BinarySearchTree myTree = new BinarySearchTree();

            int[] treeKey = { 69, 420, 77, 80, 13, 1, 8, 666, 71, 100 };
            for (int i = 0; i < treeKey.Length; ++i)
            {
                myTree.addLeaf(treeKey[i]);
            }
            myTree.printInOrder();
            Console.WriteLine();
            Console.WriteLine("The smallest value in the tree is {0}", myTree.findSmallest());
            myTree.removeNode(3);
            myTree.removeNode(69);
            myTree.removeNode(13);
            myTree.printInOrder();
            Console.WriteLine();

            //Stack

            Console.WriteLine("---Stack---");
            StackClass myStack = new StackClass();

            myStack.push("Bob", 5);
            myStack.push("Amy", 69);
            myStack.push("John wick", 100);
            myStack.push("Nice", 420);
            myStack.print();
            myStack.pop();
            myStack.pop();
            myStack.print();

            //Hash Table

            Console.WriteLine("---Hash Table---");
            HashTable myTable = new HashTable(10);

            myTable.addItem("Paul", "Locha");
            myTable.addItem("Kim", "Iced mocha");
            myTable.addItem("Emma", "Strawberry smoothie");
            myTable.addItem("Annie", "Hot Chocolate");
            myTable.addItem("Sarah", "Passion Tea");
            myTable.addItem("Pepper", "Caramel Mocha");
            myTable.addItem("Mike", "Chai Tea");
            myTable.addItem("Steve", "Apple Cider");
            myTable.addItem("Bill", "Root Beer");
            myTable.addItem("Marie", "Skinny Latte");
            myTable.addItem("Susan", "Water");
            myTable.addItem("Joe", "Green Tea");
            myTable.printTable();
            myTable.printItemsInIndex(4);
            myTable.removeItem("Marie");
            myTable.printItemsInIndex(4);
        }
        public void TestDoublyLinkedList()
        {
            int retVal;
            DoublyLinkedList dll = new DoublyLinkedList(5);

            // Push()
            dll.DeleteAt(0);
            dll.Push(1);    // LL is empty
            Assert.AreEqual(new List<int>() { 1 }, dll.ReturnList());
            dll.Push(2);    // LL.count == 1
            Assert.AreEqual(new List<int>() { 2, 1 }, dll.ReturnList());
            dll.Push(3);    // LL.count > 1
            Assert.AreEqual(new List<int>() { 3, 2, 1 }, dll.ReturnList());

            // Delete()
            dll.InitializeDLL();
            retVal = dll.Delete(111); // doesn't exist
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1,0 }, dll.ReturnList());
            Assert.AreEqual(-1, retVal);
            retVal = dll.Delete(0); // exists at tail
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(0, retVal);
            retVal = dll.Delete(5); // exists at head
            Assert.AreEqual(new List<int>() { 4, 3, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(5, retVal);
            retVal = dll.Delete(3); // exists between head and tail
            Assert.AreEqual(new List<int>() { 4, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(3, retVal);

            // DeleteAt()
            dll.InitializeDLL();
            retVal = dll.DeleteAt(-23); // invalid index
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1, 0 }, dll.ReturnList());
            Assert.AreEqual(-1, retVal);
            retVal = dll.DeleteAt(7); // invalid index
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1, 0 }, dll.ReturnList());
            Assert.AreEqual(-1, retVal);
            retVal = dll.DeleteAt(0); // exists at head
            Assert.AreEqual(new List<int>() { 4, 3, 2, 1, 0 }, dll.ReturnList());
            Assert.AreEqual(5, retVal);
            retVal = dll.DeleteAt(4); // exists at tail
            Assert.AreEqual(new List<int>() { 4, 3, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(0, retVal);
            retVal = dll.DeleteAt(1); // exists between head and tail
            Assert.AreEqual(new List<int>() { 4, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(3, retVal);

            // Search()
            dll.InitializeDLL();
            Assert.AreEqual(-1, dll.Search(111)); // doesn't exist
            Assert.AreEqual(5, dll.Search(5)); // exists at head
            Assert.AreEqual(0, dll.Search(0)); // exists at tail
            Assert.AreEqual(3, dll.Search(3)); // exists in between head and tail
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1, 0 }, dll.ReturnList());

            // Append()
            dll = new DoublyLinkedList(0);
            dll.DeleteAt(0);
            dll.Append(0); // LL is empty
            Assert.AreEqual(new List<int>() { 0 }, dll.ReturnList());
            dll.Append(1); // LL.count = 1
            Assert.AreEqual(new List<int>() { 0, 1 }, dll.ReturnList());
            dll.Append(2);
            Assert.AreEqual(new List<int>() { 0, 1, 2 }, dll.ReturnList());
            dll.Append(3); // LL.count > 1
            Assert.AreEqual(new List<int>() { 0, 1, 2, 3 }, dll.ReturnList());

            // Pop()
            dll = new DoublyLinkedList(0);
            dll.Append(1);
            dll.Append(2);
            Assert.AreEqual(2, dll.Pop()); // LL.count > 1;
            Assert.AreEqual(new List<int>() { 0, 1 }, dll.ReturnList());
            Assert.AreEqual(1, dll.Pop()); // LL.count > 1;
            Assert.AreEqual(new List<int>() { 0 }, dll.ReturnList());
            Assert.AreEqual(0, dll.Pop()); // LL.count == 1;
            Assert.AreEqual(new List<int>(), dll.ReturnList());
            Boolean isException = false;
            try
            {
                dll.Pop();
            }
            catch
            {
                isException = true;
            }
            Assert.IsTrue(isException);
        }
 internal ListElement(string value, DoublyLinkedList ownerList)
 {
     Value     = value;
     OwnerList = ownerList;
 }