Exemplo n.º 1
0
        public void PostOrder(MyStack <int> treestack, Node current)
        {
            if (current.Left != null)
            {
                PostOrder(treestack, current.Left);
            }


            if (current.Right != null)
            {
                PostOrder(treestack, current.Right);
            }

            treestack.push(current.Value);
        }
Exemplo n.º 2
0
        public int FindMaxValue()
        {
            if (Root == null)
            {
                throw new TreeEmptyException();
            }

            MyStack <int> maxStack = new MyStack <int>();

            maxStack.push(0);
            Node current = Root;

            FindMaxValue_PreOrder(maxStack, current);

            return(maxStack.Top.Value);
        }
Exemplo n.º 3
0
        public void FindMaxValue_PreOrder(MyStack <int> maxStack, Node current)
        {
            if (current.Value > maxStack.Top.Value)
            {
                maxStack.pop();
                maxStack.push(current.Value);
            }

            if (current.Left != null)
            {
                PreOrder(maxStack, current.Left);
            }
            if (current.Right != null)
            {
                PreOrder(maxStack, current.Right);
            }
        }
Exemplo n.º 4
0
        public IEnumerable <T> PostOrder()
        {
            if (this.root == null)
            {
                yield break;
            }

            MyStack <TreeNode <T> > stack = new MyStack <TreeNode <T> >();

            TreeNode <T> current = null;
            TreeNode <T> parent  = null;

            stack.Push(this.root);

            do
            {
                current = stack.Top;

                if (parent == null || parent.Left == current || parent.Right == current)
                {
                    if (current.Left != null)
                    {
                        stack.Push(current.Left);
                    }
                    else if (current.Right != null)
                    {
                        stack.Push(current.Right);
                    }
                }
                else if (parent == current.Left)
                {
                    if (current.Right != null)
                    {
                        stack.Push(current.Right);
                    }
                }
                else
                {
                    yield return(current.Value);

                    stack.Pop();
                }

                parent = current;
            }while (stack.Count > 0);
        }
Exemplo n.º 5
0
        public void AddToStackList(int data)
        {
            Node    node    = new Node(data);
            MyStack myStack = new MyStack();

            if (listOfStackPlates.Count == 0)
            {
                listOfStackPlates.Add(myStack);
            }
            var currentStack = listOfStackPlates[listOfStackPlates.Count - 1];

            if (currentStack.count < this.stackSize)
            {
                AddNodeToTheTop(ref currentStack.top, data);
                currentStack.count++;
            }
        }
Exemplo n.º 6
0
        public int[] DepthHandler(string choice)
        {
            if (Root == null)
            {
                throw new TreeEmptyException();
            }

            int[]         result    = new int[Count];
            MyStack <int> treeStack = new MyStack <int>();
            Node          current   = Root;

            switch (choice)
            {
            case "preorder":
                PreOrder(treeStack, current);
                break;

            case "inorder":
                InOrder(treeStack, current);
                break;

            case "postorder":
                PostOrder(treeStack, current);
                break;

            default:
                break;
            }

            for (int i = (Count - 1); i >= 0; i--)
            {
                result[i] = treeStack.pop();
            }

            return(result);
        }
Exemplo n.º 7
0
        public static void palindrome()
        {
            MyStack ss = new MyStack();

            Console.WriteLine("give me a string");
            string str = Console.ReadLine();

            //convert string into character array ie split into individual character
            char[] ch = str.ToCharArray();
            //then conver char array to string array
            String[] str2 = new string[str.Length];

            for (int i = 0; i < str.Length; i++)
            {
                str2[i] = ch[i].ToString();
            }
            //push the array of string into the stack
            for (int i = 0; i < str.Length; i++)
            {
                ss.StackPush(str2[i]);
            }
            // get the character from the stack and then compare

            string c = ss.AbstractFStack();

            int cc = c.CompareTo(str);

            if (cc == 0)
            {
                Console.WriteLine("given string is a palindrome");
            }
            else
            {
                Console.WriteLine("given string is not a palindrome");
            }
        }
Exemplo n.º 8
0
        static void StackTest()
        {
            int           N     = 20;
            MyStack <int> stack = new MyStack <int>();

            Console.WriteLine("\n Stack is empty: " + stack.isEmpty());

            for (int i = 0; i < N; i++)
            {
                Console.WriteLine("Try to PUSH num: " + i);
                stack.Push(i);
            }

            Console.WriteLine("\n Stack is empty: " + stack.isEmpty());

            for (int i = 0; i < N; i++)
            {
                Console.WriteLine("Try to PULL, number is: " + stack.Pop());
            }

            Console.WriteLine("\n Stack is empty: " + stack.isEmpty());

            Console.Read();
        }
Exemplo n.º 9
0
        public static void  ExecStack()
        {
            MyStack myStack = new MyStack();

            myStack.PrintStack();

            Console.WriteLine(myStack.isEmpty());

            myStack.push("exec print");

            myStack.push("alloc 11 bytes");

            myStack.push(91);

            myStack.PrintStack();

            myStack.peek();

            Console.WriteLine(myStack.isEmpty());

            myStack.pop();

            myStack.PrintStack();
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            //variables:
            bool                     bRepeatFullMenu = true, bRepeatInnerMenu = true;
            int                      iMainMenuInput = 0, iInnerMenuInput = 0;
            Stack <string>           my_stack = new Stack <string>();
            Queue <string>           my_queue = new Queue <string>();
            Dictionary <string, int> my_dictionary = new Dictionary <string, int>();
            string                   search_string = "", add_string = "", delete_string = "";


            while (bRepeatFullMenu) //full outer menu loop
            {
                //main menu input loop
                bRepeatFullMenu = true;
                while (bRepeatFullMenu)
                {
                    //display the main menu
                    Console.WriteLine("\nPlease make a selection: ");
                    printMainMenu();

                    try
                    {
                        iMainMenuInput = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine();
                        bRepeatFullMenu = false;

                        //confirm inner menu input
                        if (iMainMenuInput == 1 || iMainMenuInput == 2 || iMainMenuInput == 3)
                        {
                            bRepeatInnerMenu = true;
                        }
                        else if (iMainMenuInput == 4)
                        {
                            Console.WriteLine("Press Enter to exit...");
                            bRepeatFullMenu  = false;
                            bRepeatInnerMenu = false;
                        }
                        else
                        {
                            bRepeatFullMenu  = true;
                            bRepeatInnerMenu = false;
                            Console.WriteLine("Please enter a valid choice\n\n");
                        }


                        while (bRepeatInnerMenu) //inner menus input loop
                        {
                            Console.WriteLine("\nPlease make a selection: ");

                            switch (iMainMenuInput) //print inner menu
                            {
                            case 1:
                                MyStack.printStackMenu();
                                break;

                            case 2:
                                MyQueue.printQueueMenu();
                                break;

                            case 3:
                                MyDictionary.printDictionaryMenu();
                                break;
                            }

                            try
                            {
                                iInnerMenuInput = Convert.ToInt32(Console.ReadLine());
                                Console.WriteLine();
                                bRepeatInnerMenu = false;

                                //inner loop input handling
                                switch (iInnerMenuInput)
                                {
                                case 1:     // add one item

                                    bRepeatInnerMenu = true;
                                    switch (iMainMenuInput)
                                    {
                                    case 1:         // stack
                                        Console.Write("Enter the string: ");
                                        add_string = Convert.ToString(Console.ReadLine());
                                        my_stack   = MyStack.addOne(my_stack, add_string);
                                        break;

                                    case 2:         // queue
                                        Console.Write("Enter the string: ");
                                        add_string = Convert.ToString(Console.ReadLine());
                                        my_queue   = MyQueue.addOne(my_queue, add_string);
                                        break;

                                    case 3:         // dictionary
                                        Console.Write("Enter the string key: ");
                                        add_string = Convert.ToString(Console.ReadLine());
                                        Console.Write("\nEnter the integer value: ");         // TODO add error handling for the integer conversion
                                        try
                                        {
                                            int add_int = Convert.ToInt32(Console.ReadLine());
                                            my_dictionary = MyDictionary.addOne(my_dictionary, add_string, add_int);
                                        }
                                        catch
                                        {
                                            Console.WriteLine("\nNothing added to the dictionary (make sure the key is not duplicate and the integer is valid.\n");
                                        }
                                        break;
                                    }

                                    break;

                                case 2:     // add huge list of items

                                    bRepeatInnerMenu = true;
                                    switch (iMainMenuInput)
                                    {
                                    case 1:         // stack
                                        my_stack = MyStack.addHugeList(my_stack);
                                        break;

                                    case 2:         // queue
                                        my_queue = MyQueue.addHugeList(my_queue);
                                        break;

                                    case 3:         // dictionary
                                        my_dictionary = MyDictionary.addHugeList(my_dictionary);
                                        break;
                                    }

                                    break;

                                case 3:     // display the list

                                    bRepeatInnerMenu = true;
                                    switch (iMainMenuInput)
                                    {
                                    case 1:         // stack
                                        MyStack.display(my_stack);
                                        break;

                                    case 2:         // queue
                                        MyQueue.display(my_queue);
                                        break;

                                    case 3:         // dictionary
                                        MyDictionary.display(my_dictionary);
                                        break;
                                    }

                                    break;

                                case 4:     // delete from the list

                                    bRepeatInnerMenu = true;
                                    switch (iMainMenuInput)
                                    {
                                    case 1:         // stack
                                        Console.Write("Enter the string to delete: ");
                                        delete_string = Convert.ToString(Console.ReadLine());
                                        my_stack      = MyStack.deleteFrom(my_stack, delete_string);
                                        break;

                                    case 2:         // queue
                                        Console.Write("Enter the string to delete: ");
                                        delete_string = Convert.ToString(Console.ReadLine());
                                        my_queue      = MyQueue.deleteFrom(my_queue, delete_string);
                                        break;

                                    case 3:         // dictionary
                                        Console.Write("Enter the string to delete: ");
                                        delete_string = Convert.ToString(Console.ReadLine());
                                        my_dictionary = MyDictionary.deleteFrom(my_dictionary, delete_string);
                                        break;
                                    }

                                    break;

                                case 5:     // clear the list

                                    bRepeatInnerMenu = true;
                                    switch (iMainMenuInput)
                                    {
                                    case 1:         // stack
                                        my_stack = MyStack.clear(my_stack);
                                        break;

                                    case 2:         // queue
                                        my_queue = MyQueue.clear(my_queue);
                                        break;

                                    case 3:         // dictionary
                                        my_dictionary = MyDictionary.clear(my_dictionary);
                                        break;
                                    }

                                    break;

                                case 6:     // search the list

                                    bRepeatInnerMenu = true;
                                    switch (iMainMenuInput)
                                    {
                                    case 1:         // stack
                                        Console.WriteLine("Enter the value you want to search for:");
                                        search_string = Convert.ToString(Console.ReadLine());
                                        my_stack      = MyStack.search(my_stack, search_string);
                                        break;

                                    case 2:         // queue
                                        Console.WriteLine("Enter the value you want to search for:");
                                        search_string = Convert.ToString(Console.ReadLine());
                                        my_queue      = MyQueue.search(my_queue, search_string);
                                        break;

                                    case 3:         // dictionary
                                        Console.WriteLine("Enter the string (key) you want to search for:");
                                        search_string = Convert.ToString(Console.ReadLine());
                                        my_dictionary = MyDictionary.search(my_dictionary, search_string);
                                        break;
                                    }

                                    break;

                                case 7:     // return to the main menu
                                    bRepeatInnerMenu = false;
                                    bRepeatFullMenu  = true;
                                    break;

                                default:     // entered an integer, but not a valid menu item 1-7
                                    bRepeatInnerMenu = true;
                                    Console.WriteLine("Please enter a valid choice\n\n");
                                    break;
                                }
                            }
                            catch
                            {
                                bRepeatInnerMenu = true;
                                Console.WriteLine("Please enter a valid integer");
                            }
                        } // end of inner menu loop
                    }
                    catch
                    {
                        bRepeatFullMenu = true;
                        Console.WriteLine("Please enter a valid integer");
                    }
                }
            } // end of main menu outer loop

            Console.Read();
        }