Beispiel #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");
        }
Beispiel #2
0
 public T DeQueue1()
 {
     if (Stack1 == null || Stack1.HeadStackNodeSingly == null)
     {
         return(default(T));
     }
     return(Stack1.Pop());
 }
Beispiel #3
0
 public void EnQueue2(T data)
 {
     if (Stack1 == null)
     {
         Stack1 = new StackLinkedList <T>();
     }
     Stack1.Push(new StackNodeSingly <T>(data));
 }
Beispiel #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());
        }
        public Node Dequeue()
        {
            while (Stack1.Top.Next != null)
            {
                Stack2.Push(Stack1.Pop());
            }
            Node wantedNode = Stack1.Pop();

            return(wantedNode);
        }
Beispiel #6
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());
        }
Beispiel #7
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());
        }
 /// <summary>
 /// This method removes the node at the "front" of the queue; which is at the bottom of stack1.
 /// </summary>
 /// <returns>This is the node at the bottom of stack 1</returns>
 public Node Dequeue()
 {
     if (Stack2.Peek() == null)
     {
         while (Stack1.Peek().Next != null)
         {
             Stack2.Push(Stack1.Pop());
         }
         Stack2.Push(Stack1.Pop());
     }
     return(Stack2.Pop());
 }
Beispiel #9
0
        static void Main(string[] args)
        {
            Stack1 objstack1 = new Stack1();
            Stack2 objstack2 = new Stack2();
            int    input     = int.Parse(Console.ReadLine());

            for (int i = 0; i < input; i++)
            {
                string   val    = Console.ReadLine();
                string[] values = val.Split(' ');
                if (values[0] == "1")
                {
                    objstack1.push(int.Parse(values[1]));
                }
                else if (values[0] == "2")
                {
                    int chk;
                    if (objstack2.empty())
                    {
                        while ((chk = objstack1.pop()) != 0)
                        {
                            objstack2.push(chk);
                        }
                    }
                    objstack2.pop();
                    //int chk2;
                    //while ((chk2= objstack2.pop()) != 0)
                    //{

                    //    objstack1.push(chk2);
                    //}
                }
                else
                {
                    int chk3;
                    if (objstack2.empty())
                    {
                        while ((chk3 = objstack1.pop()) != 0)
                        {
                            objstack2.push(chk3);
                        }
                    }
                    Console.WriteLine(objstack2.print());
                    //int chk4;
                    //while ((chk4=objstack2.pop() )!= 0)
                    //{

                    //    objstack1.push(chk4);
                    //}
                }
            }
            Console.ReadKey();
        }
 /// <summary>
 /// while theres more than one node in Stack1, pop them off into stack2, then return
 /// the last node in stack1, then put all the stack2 nodes back into stack1
 /// </summary>
 /// <returns></returns>
 public Node Dequeue()
 {
     while (Stack1.Top != null)
     {
         Stack2.Push(Stack1.Pop());
         if (Stack1.Top.Next == null)
         {
             Stack2.Push(Stack1.Pop());
         }
     }
     return(Stack2.Pop());
 }
Beispiel #11
0
 /// <summary>
 /// Remove and return the "front" of the queue, which is actually the top of the primary stack.
 /// </summary>
 /// <returns>Front node in queue</returns>
 public string Dequeue()
 {
     if (Stack1.Top == null)
     {
         throw new Exception("Empty pseudoqueue");
     }
     else
     {
         string result = Stack1.Pop();
         Front = Stack1.Top;
         return(result);
     }
 }
Beispiel #12
0
        public T DeQueue2()
        {
            if (Stack2 == null)
            {
                Stack2 = new StackLinkedList <T>();
            }

            while (Stack1.HeadStackNodeSingly != null)
            {
                Stack2.Push(new StackNodeSingly <T>(Stack1.Pop()));
            }

            return(Stack2.Pop());
        }
 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);
     }
 }
Beispiel #14
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);
        }
Beispiel #15
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);
        }
        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);
            }
        }
Beispiel #17
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);
        }
Beispiel #19
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;
     }
 }
Beispiel #20
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);
        }
        //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);
        }
Beispiel #22
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);
        }
Beispiel #23
0
        private void btnStack_Click(object sender, EventArgs e) //STACK BUTTON
        {
            //Declaring a new instance of Stacks and displaying properties to the Windows Form
            Stack1 stack1 = new Stack1();

            richTextBox2.AppendText(stack1.AccessTime + "        ");
            richTextBox2.AppendText(stack1.SearchTime + "\n");
            richTextBox2.AppendText(stack1.InsertTime + "        ");
            richTextBox2.AppendText(stack1.DeleteTime + "\n");
            richTextBox2.AppendText(stack1.SpaceComplexity + "\n");
            richTextBox2.AppendText("*All Big-O values represent worst-case scenarios unless otherwise noted");

            //Displaying the Advantages and Disadvantages of Stacks to the Windows Form
            richTextBox1.AppendText("STACK: \n");
            richTextBox1.AppendText(stack1.Advantages("A stack stores data in the same way that arrays do—it’s simply a list of elements." +
                                                      "A stack uses LIFO (last -in first -out) ordering.\n" +
                                                      "It uses the following operations:\n" +
                                                      "   -pop(): Remove the top item from the stack.\n" +
                                                      "   -push(item): Add an item to the top of the stack.\n" +
                                                      "   -peek(): return the top of the stack.\n" +
                                                      "   -isEmpty(): Return true if and only if the stack is empty.\n" +
                                                      "Stacks are useful in certain recursive algorithms.  And allows constant time adds and removes."));
            richTextBox1.AppendText("\n\n");
            richTextBox1.AppendText(stack1.Disadvantages("Disadvantages of a stack: \n" +
                                                         "   -Stack memory is very limited.\n" +
                                                         "   -Creating too many objects on the stacks can increase the risk of stack overflow. \n" +
                                                         "   -Random access is not possible. \n" +
                                                         "   -Variable storage will be overwritten, which sometimes leads to undefined behavior of the function or program."));

            //Displaying extra notes from a rich text file in the bin
            using (StreamReader Reader = new StreamReader(@"C:\Users\Clayt\source\repos\CSC205\CSC205_StudyProject\CSC205_StudyProject\bin\Stack.rtf"))
            {
                while (!Reader.EndOfStream)
                {
                    richTextBox5.AppendText(Reader.ReadLine());
                }
            }
        }
Beispiel #24
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}");
            }
        }
Beispiel #25
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()));
            }
        }
 public void Enqueue(Node node)
 {
     Stack1.Push(node);
     Console.WriteLine(node.Value);
 }
 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);
 }
        /// <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);
        }