Queue 和Stack 的区别

        protected void Page_Load(object sender, EventArgs e)

        {

            //queue 是队列,遵循先进先出的原则

            //queue 属性 count

            // queue method Clear()   //清空 

                        //Contains() //是否包含 

                        //Dequeue() //出列 

                        //Enqueue() //入列 



            Queue queue = new Queue();

            queue.Enqueue("abc");

            queue.Enqueue(123);

            queue.Enqueue(true);



            string str = "";

            //foreach (object obj in queue)

            //{

            //    str += obj.ToString() + "\t";

            //}

            //Response.Write(str);



            //queue 先进先出。所以结果是123 true

            string str1 = queue.Dequeue().ToString();

            //foreach (object obj in queue)

            //{

            //    str +="<br/>"+ obj.ToString() + "\n";

            //}

            //Response.Write(str);



            //string str2 = queue.Peek().ToString();

            //foreach (object obj in queue)

            //{

            //    str += "<br/>" + obj.ToString() + "\n";

            //}

            //Response.Write(str);//结果123 true



            //Stack 的主要成员:

            ///* 属性 */ 

            //Count       // 



            ///* 方法 */ 

            //Clear()      // 

            //Contains()    // 

            //Peek()       //获取将要出栈的 

            //Pop()       //出栈 

            //Push()       //压栈 







            Stack stack = new Stack();

            stack.Push("abc");

            stack.Push("efg");

            stack.Push("hhhs");



            //foreach (object obj in stack)

            //{

            //    str += obj.ToString() + "\t";

            //}

            //Response.Write(str);





            stack.Peek();

            foreach (object obj in stack)

            {

                str += obj.ToString() + "\t";

            }

            Response.Write(str);

            //或许先出栈的元素

            string stack1 = stack.Pop().ToString();

            Response.Write("<br/>"+stack1);



            //或许将要出栈的元素

            string stack2 = stack.Peek().ToString();

            Response.Write("<br/>" + stack2);//eft



            int n = stack.Count;

            Response.Write("<br>"+n);//2   hhhs 已经出栈

        }

你可能感兴趣的:(Queue)