Ejemplo n.º 1
0
        static void TestQueue()
        {
            Queue <int> i = new Queue <int>(5);

            i.Enqueue(4);
            i.Enqueue(3);
            i.Enqueue(-4);
            i.Enqueue(6);
            i.Enqueue(7);

            while (!i.IsEmpty())
            {
                Console.WriteLine(i.Dequeue());
                Console.ReadKey();
            }
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //FIFO
            Queue queue = new Queue();

            Console.WriteLine(queue.IsEmpty());
            queue.Add(1);
            queue.Add(3);
            queue.Print();
            Console.WriteLine(queue.Peek());
            queue.Add(5);
            queue.Add(2);
            queue.Print();
            queue.Remove();
            queue.Print();

            //LIFO
            Stack stack = new Stack();

            Console.WriteLine(stack.IsEmpty());
            stack.Push(1);
            stack.Push(3);
            stack.Print();
            Console.WriteLine(stack.Peek());
            stack.Push(5);
            stack.Push(2);
            stack.Print();
            stack.Pop();
            stack.Print();

            Console.ReadKey();

            LinkedList linkedList = new LinkedList(1);

            linkedList.PrindList();
            linkedList.Delete();
            linkedList.Append(2);
            linkedList.PrindList();
            linkedList.Append(4);
            linkedList.Append(10);
            linkedList.PrindList();
            linkedList.Delete();
            linkedList.PrindList();

            linkedList.Clear();
            linkedList.PrindList();

            linkedList.Append(2);
            linkedList.Append(4);
            linkedList.Append(10);
            linkedList.PrindList();
            linkedList.Delete(4);
            linkedList.PrindList();

            linkedList.Clear();
            linkedList.PrindList();

            linkedList.Append(2);
            linkedList.Append(4);
            linkedList.Append(10);
            linkedList.PrindList();
            linkedList.Delete(2);
            linkedList.PrindList();

            linkedList.Clear();
            linkedList.PrindList();

            linkedList.Append(2);
            linkedList.Append(4);
            linkedList.Append(10);
            linkedList.PrindList();
            linkedList.Delete(10);
            linkedList.PrindList();

            Console.ReadKey();

            Console.WriteLine("Hello World!");

            HeapMin heapMin = new HeapMin();

            heapMin.printArray = true;
            heapMin.Add(3);
            heapMin.Add(4);
            heapMin.Add(8);
            heapMin.Add(1);
            heapMin.Add(7);
            heapMin.Add(2);
            heapMin.Add(5);

            int min = heapMin.Peek();

            Console.WriteLine(min); // should == 1
            heapMin.Pool();
            heapMin.Pool();
            heapMin.Pool();
            min = heapMin.Peek();
            Console.WriteLine(min); // should == 4


            //Console.ReadKey();

            //BSTree bSTree = new BSTree(10);
            //bSTree.Insert(15);
            //bSTree.Insert(5);
            //bSTree.PrintInOrder();
            //bSTree.Insert(8);
            //bSTree.PrintInOrder();
            //Console.WriteLine(bSTree.Contains(8));
            //Console.WriteLine(bSTree.Contains(18));


            //Console.ReadKey();

            string[] keys = { "the", "a",  "there", "answer",
                              "any", "by", "bye",   "their" };

            string[] output = { "Not present in trie", "Present in trie" };

            Trie trie = new Trie();

            // Construct trie
            for (int i = 0; i < keys.Length; i++)
            {
                trie.Insert(keys[i]);
            }

            // Search for different keys
            if (trie.Contains("the") == true)
            {
                Console.WriteLine("the --- " + output[1]);
            }
            else
            {
                Console.WriteLine("the --- " + output[0]);
            }

            if (trie.Contains("these") == true)
            {
                Console.WriteLine("these --- " + output[1]);
            }
            else
            {
                Console.WriteLine("these --- " + output[0]);
            }

            if (trie.Contains("their") == true)
            {
                Console.WriteLine("their --- " + output[1]);
            }
            else
            {
                Console.WriteLine("their --- " + output[0]);
            }

            if (trie.Contains("thaw") == true)
            {
                Console.WriteLine("thaw --- " + output[1]);
            }
            else
            {
                Console.WriteLine("thaw --- " + output[0]);
            }

            if (trie.Contains("thei") == true)
            {
                Console.WriteLine("thei --- " + output[1]);
            }
            else
            {
                Console.WriteLine("thei --- " + output[0]);
            }

            if (trie.Contains("thei", true) == true)
            {
                Console.WriteLine("thei (full word search) --- " + output[1]);
            }
            else
            {
                Console.WriteLine("thei (full word search) --- " + output[0]);
            }

            Console.ReadKey();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            int choice;
            int innerChoice = 0;

            Console.WriteLine("------------------------Data Structures--------------------------");
            Console.WriteLine("1. Stack");
            Console.WriteLine("2. Queue");
            Console.WriteLine("3. Linked List");
            int.TryParse(Console.ReadLine(), out choice);
            switch (choice)
            {
            case 1:
                Stack stack = new Stack();
                while (true)
                {
                    Console.WriteLine("1. Add");
                    Console.WriteLine("2. Remove");
                    Console.WriteLine("3. Display");
                    Console.WriteLine("4. Sort");
                    Console.WriteLine("5. Get Top Value");
                    Console.WriteLine("6. Exit from Stack");

                    choice = int.Parse(Console.ReadLine());
                    switch (choice)
                    {
                    case 1:
                        Console.WriteLine("Enter Element to be Inserted");
                        stack.Add(int.Parse(Console.ReadLine()));
                        break;

                    case 2:
                        Console.WriteLine("Removed " + stack.Remove() + " from the top of the stack");
                        break;

                    case 3:
                        stack.Display();
                        break;

                    case 4:
                        stack.Sort();
                        Console.WriteLine("Sorted Stack");
                        stack.Display();
                        break;

                    case 5:
                        Console.WriteLine("The Top Value of Stack=" + stack.GetTop());
                        break;

                    case 6:
                        innerChoice = 1;
                        break;

                    default:
                        Console.WriteLine("Wrong Choice");
                        break;
                    }
                    if (innerChoice == 1)
                    {
                        innerChoice = 0;
                        break;
                    }
                }
                break;

            case 2:
                Queue queue = new Queue();
                while (true)
                {
                    Console.WriteLine("1. Add");
                    Console.WriteLine("2. Remove");
                    Console.WriteLine("3. Display");
                    Console.WriteLine("4. Sort");
                    Console.WriteLine("5. Peek");
                    Console.WriteLine("6. Check Queue is Full");
                    Console.WriteLine("7. Check Queue is Empty");
                    Console.WriteLine("8. Exit from Queue");
                    choice = int.Parse(Console.ReadLine());
                    switch (choice)
                    {
                    case 1:
                        Console.WriteLine("Enter Element to be Inserted");
                        queue.Add(int.Parse(Console.ReadLine()));
                        Console.WriteLine("Data Inserted");
                        break;

                    case 2:
                        Console.WriteLine("Removed " + queue.Remove() + " from the front of the queue");
                        break;

                    case 3:
                        queue.Display();
                        break;

                    case 4:
                        queue.Sort();
                        Console.WriteLine("Sorted Queue");
                        queue.Display();
                        break;

                    case 5:
                        Console.WriteLine("The Front Value of Queue=" + queue.Peek());
                        break;

                    case 6:
                        if (queue.IsFull())
                        {
                            Console.WriteLine("Queue is full");
                        }
                        else
                        {
                            Console.WriteLine("Queue is not full");
                        }
                        break;

                    case 7:
                        if (queue.IsEmpty())
                        {
                            Console.WriteLine("Queue is empty");
                        }
                        else
                        {
                            Console.WriteLine("Queue is not empty");
                        }
                        break;

                    case 8:
                        innerChoice = 1;
                        break;

                    default:
                        Console.WriteLine("Wrong Choice");
                        break;
                    }

                    if (innerChoice == 1)
                    {
                        innerChoice = 0;
                        break;
                    }
                }
                break;

            case 3:
                LinkedList linkedList = new LinkedList();
                while (true)
                {
                    Console.WriteLine("1. Add");
                    Console.WriteLine("2. Remove");
                    Console.WriteLine("3. Display");
                    Console.WriteLine("4. Sort");
                    Console.WriteLine("5. Insert at specific location");
                    Console.WriteLine("6. Remove from specific location");
                    Console.WriteLine("7. Exit from Queue");
                    choice = int.Parse(Console.ReadLine());
                    switch (choice)
                    {
                    case 1:
                        Console.WriteLine("Enter Element to be Inserted");
                        linkedList.Add(int.Parse(Console.ReadLine()));
                        Console.WriteLine("Data Inserted");
                        break;

                    case 2:
                        Console.WriteLine("Removed " + linkedList.Remove() + " from the linked list");
                        break;

                    case 3:
                        linkedList.Display();
                        break;

                    case 4:
                        linkedList.Sort();
                        Console.WriteLine("Sorted Linked List");
                        linkedList.Display();
                        break;

                    case 5:
                        Console.WriteLine("Enter Data");
                        int requestData = int.Parse(Console.ReadLine());
                        Console.WriteLine("Enter Index");
                        int index = int.Parse(Console.ReadLine());
                        linkedList.AddDataAtPosition(requestData, index);
                        Console.WriteLine("Data Inserted");
                        break;

                    case 6:
                        Console.WriteLine("Enter Index");
                        int responseData = linkedList.RemoveAtIndex(int.Parse(Console.ReadLine()));
                        Console.WriteLine("Data Removed");
                        break;

                    case 7:
                        innerChoice = 1;
                        break;

                    default:
                        Console.WriteLine("Wrong Choice");
                        break;
                    }

                    if (innerChoice == 1)
                    {
                        innerChoice = 0;
                        break;
                    }
                }
                break;

            default:
                break;
            }
        }