Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Stack <string> stack = new Stack <string>();
            Queue <string> q     = new Queue <string>();
            Bag <string>   b     = new Bag <string>();
            Deque <string> deque = new Deque <string>();

            while (!StdIn.IsEmpty())
            {
                string item = StdIn.ReadString();
                b.Add(item);
                stack.Push(item);
                deque.PushLeft(item);
                if (!item.Equals("-"))
                {
                    q.Enqueue(item);
                }
                //else if (!q.IsEmpty)
                //StdOut.Print(q.Dequeue() + " ");
            }

            stack.Reverse();
            Queue <string> q2 = new Queue <string>(q);

            q.Reverse();
            while (!q2.IsEmpty)
            {
                StdOut.Print(q2.Dequeue() + " ");
            }
            while (!q.IsEmpty)
            {
                StdOut.Print(q.Dequeue() + " ");
            }

            Stack <string> stack2 = new Stack <string>(stack);

            while (!stack.IsEmpty)
            {
                StdOut.Print(stack.Pop() + " ");
            }
            while (!stack2.IsEmpty)
            {
                StdOut.Print(stack2.Pop() + " ");
            }

            StdOut.Println();

            while (!deque.IsEmpty)
            {
                StdOut.Print(deque.PopRight() + " ");
            }
            StdOut.Println();
            foreach (var item in b)
            {
                StdOut.Print(item + " ");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Unit tests the <tt>Stack</tt> data type.
        /// </summary>
        /// <param name="args"></param>
        public static void Main(String[] args)
        {
            Stack <String> s = new Stack <String>();

            while (!StdIn.IsEmpty())
            {
                String item = StdIn.ReadString();
                if (!item.Equals("-"))
                {
                    s.Push(item);
                }
                else if (!s.IsEmpty())
                {
                    Console.WriteLine(s.Pop() + " ");
                    Debug.WriteLine(s.Pop() + " ");
                }
            }
            Console.WriteLine("(" + s.Size + " left on stack)");
            Debug.WriteLine("(" + s.Size + " left on stack)");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Unit tests the Queue data type.
        /// </summary>
        /// <param name="args"></param>
        public static void Main(String[] args)
        {
            Queue <String> q = new Queue <String>();

            while (!StdIn.IsEmpty())
            {
                String item = StdIn.ReadString();
                if (!item.Equals("-"))
                {
                    q.Enqueue(item);
                }
                else if (!q.IsEmpty())
                {
                    Console.WriteLine(q.Dequeue() + " ");
                    Debug.WriteLine(q.Dequeue() + " ");
                }
            }
            Console.WriteLine("(" + q.Size + " left on queue)");
            Debug.WriteLine("(" + q.Size + " left on queue)");
        }