zoukankan      html  css  js  c++  java
  • c#中的栈(stack)与队列(queue)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace StackAndQueue
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //栈是先进后出
    14             Stack<string> fruits=new Stack<string>();
    15 
    16             //元素入栈用push,就是向栈中添加元素
    17             fruits.Push("Apple");
    18             fruits.Push("pear");
    19             fruits.Push("grape");
    20             fruits.Push("watermelon");
    21 
    22             //获取stack 中的元素个数
    23             int num = fruits.Count();
    24 
    25             //pop出栈,返回最最后入栈的元素并删除栈中的元素
    26             string s1 = fruits.Pop();
    27 
    28             //Peek返回最后入栈的元素,不删除栈中的元素
    29             string s2 = fruits.Peek();
    30 
    31             //清空栈中元素
    32             fruits.Clear();
    33 
    34 
    35 
    36 
    37 
    38             //队列与栈不同,遵循先进先出的原则
    39             Queue<string> name = new Queue<string> ( );
    40 
    41             //入队列,向队列中添加元素
    42             name.Enqueue ("" );
    43             name.Enqueue ("");
    44             name.Enqueue ("" );
    45             name.Enqueue ( "");
    46 
    47             //获取队列中的元素个数
    48             int a=name.Count ( );
    49 
    50             //判断指定元素是否包含在队列中
    51             bool b=name.Contains ("");
    52 
    53             //出队列,使用Dequeue出队列会获取队列中最下方的元素,也就是先进入的元素,然后删除队列中的该元素
    54             string str1= name.Dequeue ( );
    55 
    56             //使用peek 出队列,只会讲最下方的元素获取,不会将元素从队列中删除
    57             string str2=name.Peek ( );
    58 
    59             //清空队列
    60             name.Clear ( );
    61         }
    62     }
    63 }
  • 相关阅读:
    python 中 print函数的用法详解
    可转债操作一览
    Python基本数据类型
    python的列表
    理财的方法
    92、Multiple commands produce Info.plist 报错
    91、最新cocoaPods安装与使用
    90、引入头文件不提示
    89、instancetype和id的区别
    88、const、static、extern介绍
  • 原文地址:https://www.cnblogs.com/wodewei/p/StackAndQueue.html
Copyright © 2011-2022 走看看