zoukankan      html  css  js  c++  java
  • 栈Stack和队列Queue

    1.Stack和Queue

    Stack是先进后出的,Queue是先进先出的。

    使用方法如下:

            public class Dog
            {
                
                public string Name { get; set; }
                public Dog(string name)
                {
                    this.Name = name;
                }
                public void ConsoleName()
                {
                    Console.WriteLine(this.Name);
                }
            }           
               //栈 先进后出
                Stack<Dog> s = new Stack<Dog>();
                s.Push(new Dog("A"));//Push插入对象
                s.Push(new Dog("B"));
                s.Peek().ConsoleName();//Peek返回顶部对象--B
                s.Pop().ConsoleName();//移除并返回顶部对象(返回对象是移除的对象)--B
                s.Peek().ConsoleName();//--A
    
                //对列 先进先出
                Queue<Dog> q = new Queue<Dog>();
                q.Enqueue(new Dog("A"));
                q.Enqueue(new Dog("B"));
                q.Peek().ConsoleName();//返回位于开始处的对象--A
                q.Dequeue().ConsoleName() ;//移除并返回开始的对象--A
                q.Peek().ConsoleName() ;//A已经移除,返回B    

    2.栈和队列的实际使用

      Stack:先进后出,饮料自动贩卖机,后放的饮料先卖。

      Queue:先进先出,如商店的存货,为防止过期,把先到的货摆上商品货架。

  • 相关阅读:
    try catch使用示例
    doxgen生成chm文档和乱码解决方法
    MFC中MessageBox()用法
    UML聚合与组合
    C#网络编程
    单元测试(NUnit)
    Autohotkey
    .NET中的并行
    System.Environment类的使用
    一键VHD
  • 原文地址:https://www.cnblogs.com/Med1tator/p/6442471.html
Copyright © 2011-2022 走看看