示例#1
0
        public void PushPopRangeTest()
        {
            //arrange
            Stack1 stack = new Stack1();
            int    result;

            stack.Push(10);
            Assert.AreEqual(stack.Max(), 10, "Value should be 10");

            stack.Push(15);
            Assert.AreEqual(stack.Max(), 15, "Value should be 15");

            stack.Push(3);
            Assert.AreEqual(stack.Max(), 15, "Value should be 15");

            stack.Push(100);
            Assert.AreEqual(stack.Max(), 100, "Value should be 100");

            stack.Pop();
            Assert.AreEqual(stack.Max(), 15, "Value should be 15");

            stack.Pop();
            Assert.AreEqual(stack.Max(), 15, "Value should be 15");

            stack.Pop();
            Assert.AreEqual(stack.Max(), 10, "Value should be 10");
        }
示例#2
0
        public void TestPeek()
        {
            Stack1 S = new Stack1();

            S.Push("1");
            S.Push("2");
            S.Push("3");
            Assert.AreEqual("3", S.Peek());
            Assert.AreEqual("2", S.Peek());
        }
示例#3
0
        public void TestClear()
        {
            Stack1 S = new Stack1();

            S.Push("1");
            S.Push("2");
            S.Push("3");
            Assert.AreEqual("3", S.Clear());
            Assert.AreEqual("3", S.Count());
        }
示例#4
0
        public void TestMethod1()
        {
            Stack1 S = new Stack1();

            S.Push("1");
            S.Push("2");
            S.Push("3");
            Assert.AreEqual("3", S.Pop());
            Assert.AreEqual("2", S.Pop());
        }
示例#5
0
        public void TestMaxWithMaxToSmallest()
        {
            //arrange
            Stack1 stack    = new Stack1();
            int    expected = 10;
            int    result;

            //act
            stack.Push(10);
            stack.Push(5);
            stack.Push(1);
            result = stack.Max();

            //assert
            Assert.AreEqual(result, expected);
        }
示例#6
0
 public void EnQueue2(T data)
 {
     if (Stack1 == null)
     {
         Stack1 = new StackLinkedList <T>();
     }
     Stack1.Push(new StackNodeSingly <T>(data));
 }
 public static void Print()
 {
     while (Stack1.Count > 0)
     {
         var element = Stack1.Pop();
         Stack2.Push(element);
     }
     Console.WriteLine("Element at the head of the queue: {0}", Stack2.Peek());
     while (Stack2.Count > 0)
     {
         var element = Stack2.Pop();
         Stack1.Push(element);
     }
 }
示例#8
0
        public void TestPushPopWithOneValue()
        {
            //arrange
            Stack1 stack    = new Stack1();
            int    val      = 5;
            int    expected = 5;
            int    result;

            //act
            stack.Push(val);
            result = stack.Pop();

            //assert
            Assert.AreEqual(result, expected);
        }
        public static void Dequeue()
        {
            while (Stack1.Count > 1)
            {
                var element = Stack1.Pop();
                Stack2.Push(element);
            }
            var dequed = Stack1.Pop();

            Console.WriteLine("Dequed element: {0}", dequed);
            while (Stack2.Count > 0)
            {
                var element = Stack2.Pop();
                Stack1.Push(element);
            }
        }
示例#10
0
        /// <summary>
        /// Mimic a Queue object's enqueue method with stacks,
        /// and returns the value what would be the front object
        /// </summary>
        /// <param name="value"> value to enqueue </param>
        /// <returns></returns>
        public T Enqueue(T value)
        {
            Stack1.Push(value);
            while (!Stack1.IsEmpty())
            {
                Stack2.Push(Stack1.Pop());
            }

            T frontValue = Stack2.Peek();

            while (!Stack2.IsEmpty())
            {
                Stack1.Push(Stack2.Pop());
            }

            return(frontValue);
        }
        /// <summary>
        /// emulates the "dequeue" method using 2 stacks
        /// </summary>
        /// <returns>the node that was "dequeued" from the stack</returns>
        public Node Dequeue()
        {
            if (Stack1.Top.Next == null)
            {
                return(Stack1.Pop());
            }
            while (Stack1.Top.Next != null)
            {
                Stack2.Push(Stack1.Pop());
            }

            Node tmpNode = Stack1.Pop();

            while (Stack2.Top.Next != null)
            {
                Stack1.Push(Stack2.Pop());
            }

            return(tmpNode);
        }
        //Dequeue() returns the first value that was placed into stack1
        public Node Dequeue()
        {
            //keep going through stack1 until we reach the bottom
            while (Stack1.Top.Next != null)
            {
                Stack2.Push(Stack1.Pop());
                //this looping mechanism will only leave one node behind...
            }
            //which is this node, so it'll be popped off
            //Since this method returns a node, we init a Node that references it
            //so we can return this node.  This is the node WE WANT
            Node returnNode = Stack1.Pop();

            //this will put all the guts of stack1 back from stack2
            while (Stack2.Top != null)
            {
                Stack1.Push(Stack2.Pop());
            }

            return(returnNode);
        }
示例#13
0
        /// <summary>
        /// Removes a node from the queue
        /// </summary>
        /// <returns>returns the node that was dequeued</returns>
        public Node Dequeue()
        {
            if (Stack2.Peek() == null)
            {
                return(null);
            }

            while (Stack2.Peek().Next != null)
            {
                Stack1.Push(Stack2.Pop());
            }

            Node temp = Stack2.Pop();

            while (Stack1.Peek() != null)
            {
                Stack2.Push(Stack1.Pop());
            }

            return(temp);
        }
示例#14
0
 /// <summary>
 /// Add a new item to the "rear" of the queue, which is actually the bottom of the primary stack
 /// </summary>
 /// <param name="val">Value to be enqueued</param>
 public void Enqueue(string val)
 {
     if (Stack1.Top == null)
     {
         Stack1.Push(val);
         Front = Stack1.Top;
     }
     else
     {
         while (Stack1.Top != null)
         {
             Stack2.Push(Stack1.Pop());
         }
         Stack1.Push(val);
         while (Stack2.Top != null)
         {
             Stack1.Push(Stack2.Pop());
         }
         Front = Stack1.Top;
     }
 }
示例#15
0
        public void TestMaxWhenWasAndPoped()
        {
            //arrange
            Stack1 stack = new Stack1();

            int[] values   = new int[] { 1, 2, 10, 5, 20 };
            int   expected = 10;
            int   result;

            //act
            foreach (int val in values)
            {
                stack.Push(val);
            }
            stack.Pop();
            stack.Pop();

            result = stack.Max();

            //assert
            Assert.AreEqual(result, expected);
        }
示例#16
0
        public void EnQueue1(T data)
        {
            if (Stack1 == null)
            {
                Stack1 = new StackLinkedList <T>();
            }

            if (Stack2 == null)
            {
                Stack2 = new StackLinkedList <T>();
            }
            var newData = new StackNodeSingly <T>(data);

            while (Stack1.HeadStackNodeSingly != null)
            {
                Stack2.Push(new StackNodeSingly <T>(Stack1.Pop()));
            }
            Stack1.Push(newData);
            while (Stack2.HeadStackNodeSingly != null)
            {
                Stack1.Push(new StackNodeSingly <T>(Stack2.Pop()));
            }
        }
示例#17
0
        /// <summary>
        /// Mimic a Queue object's dequeue method with stacks.
        /// Pop() what would be the front node and return the value
        /// </summary>
        /// <returns></returns>
        public T Dequeue()
        {
            try
            {
                while (!Stack1.IsEmpty())
                {
                    Stack2.Push(Stack1.Pop());
                }

                T frontValue = Stack2.Pop();

                while (!Stack2.IsEmpty())
                {
                    Stack1.Push(Stack2.Pop());
                }

                return(frontValue);
            }
            catch (NullReferenceException e)
            {
                throw new NullReferenceException($"Your stack is empty, womp womp\n{e}");
            }
        }
 public static void Enqueue(int x)
 {
     Stack1.Push(x);
 }
 //Enqueue(value) inserts a value to the top of the stack
 public void Enqueue(Node node)
 {
     Stack1.Push(node);
     Console.WriteLine($"THe return node is {node.Value}");
 }
 /// <summary>
 /// the new node in the Queue is whatever was on top of Stack1.
 /// </summary>
 /// <param name="value">value is whatever value in the stack you want
 /// to add to the queue
 /// </param>
 /// <returns></returns>
 public Node Enqueue(int value)
 {
     Stack1.Push(new Node(value));
     return(Stack1.Top);
 }
 public void Enqueue(Node node)
 {
     Stack1.Push(node);
     Console.WriteLine(node.Value);
 }
        /// <summary>
        /// This method takes in a value and then adds it to the "rear" of the queue; which is the top of stack1.
        /// </summary>
        /// <param name="value">This is the value that is added to the "rear" of the queue</param>
        public void Enqueue(int value)
        {
            Node node = new Node(value);

            Stack1.Push(node);
        }