Esempio n. 1
0
        /// <summary>
        /// Enqueues a value into the stack with a FIFO approach
        /// </summary>
        /// <param name="val">Value to enqueue</param>
        public void Enqueue(T val)
        {
            // if stack is empty
            if (MyStack.Top == null)
            {
                MyStack.Push(val);
            }
            else
            {
                StacksAndQueues.Stack <T> tempStack = new StacksAndQueues.Stack <T>();

                // reverse the stack and store in tempStack
                while (MyStack.Top != null)
                {
                    tempStack.Push(MyStack.Pop());
                }

                tempStack.Push(val);

                // re-reverse stack back to original state with added value
                while (tempStack.Top != null)
                {
                    MyStack.Push(tempStack.Pop());
                }
            }
        }
Esempio n. 2
0
        private static void StackImplementation()
        {
            StacksAndQueues.Stack <int> stack = new StacksAndQueues.Stack <int>(10);
            stack.Push(10);
            stack.Push(20);
            stack.Push(30);
            stack.Push(40);
            stack.Push(50);
            while (!stack.IsEmpty())
            {
                var value = stack.Pop();
                Console.WriteLine($"Popped value {value}!");
            }

            string input   = Console.ReadLine();
            int    size    = input.Length;
            string reverse = string.Empty;

            ReverseWord <char> rw = new ReverseWord <char>(size);

            Console.WriteLine($"Reversing of string started for {input}!");
            for (int i = 0; i < input.Length; i++)
            {
                var ch = input[i];
                rw.Push(ch);
            }
            while (!rw.IsEmpty())
            {
                reverse += rw.Pop();
            }
            Console.WriteLine($"Reversal of the string is : {reverse}");
        }
        /// <summary>
        /// MultiBracketValidation - Method to evaluate strings and contained bracket pairs
        /// </summary>
        /// <param name="input">The string you want to evaluate</param>
        /// <returns>A boolean representing whether or not the brackets in the string are complementary</returns>
        public static bool MultiBracketValidationMethod(string input)
        {
            char openingRound  = '(';
            char closingRound  = ')';
            char openingSquare = '[';
            char closingSquare = ']';
            char openingCurly  = '{';
            char closingCurly  = '}';

            bool hasOpeningRound  = input.Contains(openingRound);
            bool hasClosingRound  = input.Contains(closingRound);
            bool hasOpeningSquare = input.Contains(openingSquare);
            bool hasClosingSquare = input.Contains(closingSquare);
            bool hasOpeningCurly  = input.Contains(openingCurly);
            bool hasClosingCurly  = input.Contains(closingCurly);

            StacksAndQueues.Stack <char> tempStack = new StacksAndQueues.Stack <char>();

            bool hasSpecialChar = false;

            var inputArray = input.ToCharArray();

            for (int i = 0; i < inputArray.Length; i++)
            {
                if (inputArray[i] == openingRound || inputArray[i] == closingRound || inputArray[i] == openingSquare || inputArray[i] == closingSquare || inputArray[i] == openingCurly || inputArray[i] == closingCurly)
                {
                    hasSpecialChar = true;
                }
            }

            try
            {
                if (input.Length < 2 || hasOpeningRound && !hasClosingRound || hasOpeningSquare && !hasClosingSquare || hasOpeningCurly && !hasClosingCurly ||
                    !hasOpeningRound && hasClosingRound || !hasOpeningSquare && hasClosingSquare || !hasOpeningCurly && hasClosingCurly || !hasSpecialChar)
                {
                    return(false);
                }
                else
                {
                    for (int i = 0; i < input.Length; i++)
                    {
                        // filter for the opening shapes
                        if (input[i] == openingRound || input[i] == openingSquare || input[i] == openingCurly)
                        {
                            tempStack.Push(input[i]);
                        }
                        else if (input[i] == closingRound || input[i] == closingSquare || input[i] == closingCurly)
                        {
                            if (input[i] == closingRound && tempStack.Top.Value == openingRound || input[i] == closingSquare && tempStack.Top.Value == openingSquare || input[i] == closingCurly && tempStack.Top.Value == openingCurly)
                            {
                                // what if this value is null?
                                tempStack.Pop();
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else if (input[input.Length - 1] == openingRound || input[input.Length - 1] == openingSquare || input[input.Length - 1] == openingCurly)
                        {
                            return(false);
                        }
                    }
                    return(tempStack.IsEmpty());
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 4
0
 public PseudoQueue()
 {
     MyStack = new StacksAndQueues.Stack <T>();
 }