コード例 #1
0
        public override T Dequeue()
        {
            if (IsEmpty())
            {
                throw new QueueUnderFlowException();
            }

            size--;

            if (stack1.IsEmpty() && shouldTransfer)
            {
                transferContents(stack2, stack1);

                return(stack1.Pop());
            }
            else if (stack2.IsEmpty() && shouldTransfer)
            {
                transferContents(stack1, stack2);

                return(stack2.Pop());
            }
            else if (!stack1.IsEmpty())
            {
                return(stack1.Pop());
            }
            else
            {
                return(stack2.Pop());
            }
        }
コード例 #2
0
        public void ResizingArrayStackTest()
        {
            var stack = new ResizingArrayStack <int>();

            stack.Push(1);
            stack.Push(2);

            Assert.IsTrue(stack.ItemCount == 2);
            Assert.IsTrue(stack.IsEmpty() == false);
            Assert.IsTrue(stack.Peek() == 2);
            Assert.IsTrue(stack.Pop() == 2);
            Assert.IsTrue(stack.ItemCount == 1);
            stack.Pop();
            Assert.IsTrue(stack.IsEmpty());

            for (int i = 0; i < 1000; i++)
            {
                stack.Push(i);
            }

            Assert.IsTrue(stack.ItemCount == 1000);

            for (int i = 0; i < 1000; i++)
            {
                stack.Pop();
            }

            Assert.IsTrue(stack.ItemCount == 0);
        }
        public void PushAndPopTest_ReferenceType()
        {
            IStack <String> stack = new ResizingArrayStack <String>();

            stack.Push("1");
            Assert.IsFalse(stack.IsEmpty());
            Assert.AreEqual("1", stack.Peek());
            Assert.AreEqual(1, stack.Size());
            String value = stack.Pop();

            Assert.IsTrue(stack.IsEmpty());
            Assert.AreEqual(0, stack.Size());
            Assert.AreEqual("1", value);
        }
        public void PushAndPopTest_ValueType()
        {
            IStack <int> stack = new ResizingArrayStack <int>();

            stack.Push(1);
            Assert.IsFalse(stack.IsEmpty());
            Assert.AreEqual(1, stack.Peek());
            Assert.AreEqual(1, stack.Size());
            int value = stack.Pop();

            Assert.IsTrue(stack.IsEmpty());
            Assert.AreEqual(0, stack.Size());
            Assert.AreEqual(1, value);
        }
コード例 #5
0
        public void ResizingArrayStack_Positive()
        {
            var stack = new ResizingArrayStack <int>();

            Assert.IsTrue(stack.IsEmpty());
            stack.Push(1);
            stack.Push(3);
            stack.Push(5);
            stack.Push(7);
            stack.Push(9);
            Assert.AreEqual(5, stack.Size());
            Assert.AreEqual(9, stack.Peek());

            Assert.AreEqual(9, stack.Pop());
            Assert.AreEqual(7, stack.Pop());
            Assert.AreEqual(3, stack.Size());
            Assert.AreEqual(5, stack.Peek());

            stack.Push(66);
            var list = new List <int> {
                66, 5, 3, 1
            };
            int index = 0;

            foreach (var i in stack)
            {
                Assert.AreEqual(list[index++], i);
            }
        }
        public void ConstructorTest()
        {
            IStack <int> stack = new ResizingArrayStack <int>();

            Assert.IsTrue(stack.IsEmpty());
            Assert.AreEqual(0, stack.Size());
            Assert.IsTrue(string.IsNullOrWhiteSpace(stack.ToString()));
        }
コード例 #7
0
 public T Dequeue()
 {
     if (_outbox.IsEmpty())
     {
         while (!_inbox.IsEmpty())
         {
             _outbox.Push(_inbox.Pop());
         }
     }
     return(_outbox.Pop());
 }
コード例 #8
0
        private bool MatchParens(string input)
        {
            var parens = new ResizingArrayStack <char>(1);

            foreach (var i in input)
            {
                switch (i)
                {
                case '[':
                case '(':
                case '{':
                    parens.Push(i);
                    break;

                case ']':
                    if (parens.IsEmpty() || parens.Pop() != '[')
                    {
                        return(false);
                    }
                    break;

                case ')':
                    if (parens.IsEmpty() || parens.Pop() != '(')
                    {
                        return(false);
                    }
                    break;

                case '}':
                    if (parens.IsEmpty() || parens.Pop() != '{')
                    {
                        return(false);
                    }
                    break;

                default:
                    break;
                }
            }
            return(true);
        }
        public void ManyPushTest()
        {
            IStack <int> stack = new ResizingArrayStack <int>();

            for (int i = 0; i < 10; i++)
            {
                stack.Push(i);
                Assert.IsFalse(stack.IsEmpty());
                Assert.AreEqual(i + 1, stack.Size());
                Assert.AreEqual(i, stack.Peek());
            }
        }
コード例 #10
0
 public IEnumerable <int> PathTo(int v)
 {
     if (HasPathTo(v))
     {
         var path = new ResizingArrayStack <int>();
         for (int i = v; i != _s; i = _pathTo[i])
         {
             path.Push(i);
         }
         path.Push(_s);
         while (!path.IsEmpty())
         {
             yield return(path.Pop());
         }
     }
     ;
 }
コード例 #11
0
        void StackTest()
        {
            var strArr = FileHandler.ReadFileAsStrArr("words3.txt");

            strArr.Show();
            var stack = new ResizingArrayStack <string>();

            //var stack = new NodeStack<string>();
            foreach (var item in strArr)
            {
                stack.Push(item);
            }
            foreach (var item in stack)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            while (!stack.IsEmpty())
            {
                Console.Write(stack.Pop() + " ");
            }
            Console.ReadKey();
        }
コード例 #12
0
        public bool Balanced(string input)
        {
            if (string.IsNullOrEmpty(input) || input.Length == 1)
            {
                return(false);
            }

            ResizingArrayStack <char> stack = new ResizingArrayStack <char>();

            for (int i = 0; i < input.Length; i++)
            {
                if (openingParenthesis(input[i]))
                {
                    stack.Push(input[i]);
                }

                if (closingParenthesis(input[i]) && !hasMatchingOpeningParenthesis(input[i], stack))
                {
                    return(false);
                }
            }

            return(stack.IsEmpty());
        }
コード例 #13
0
 private bool hasMatchingOpeningParenthesis(char openParen, ResizingArrayStack <char> stack)
 {
     return(!stack.IsEmpty() && stack.Pop() == parenthesesPair[openParen]);
 }