c#中的栈(stack)与队列(queue)
2024-10-08 12:14:38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StackAndQueue
{
class Program
{
static void Main(string[] args)
{
//栈是先进后出
Stack<string> fruits=new Stack<string>(); //元素入栈用push,就是向栈中添加元素
fruits.Push("Apple");
fruits.Push("pear");
fruits.Push("grape");
fruits.Push("watermelon"); //获取stack 中的元素个数
int num = fruits.Count(); //pop出栈,返回最最后入栈的元素并删除栈中的元素
string s1 = fruits.Pop(); //Peek返回最后入栈的元素,不删除栈中的元素
string s2 = fruits.Peek(); //清空栈中元素
fruits.Clear(); //队列与栈不同,遵循先进先出的原则
Queue<string> name = new Queue<string> ( ); //入队列,向队列中添加元素
name.Enqueue ("一" );
name.Enqueue ("二");
name.Enqueue ("三" );
name.Enqueue ( "四"); //获取队列中的元素个数
int a=name.Count ( ); //判断指定元素是否包含在队列中
bool b=name.Contains ("三"); //出队列,使用Dequeue出队列会获取队列中最下方的元素,也就是先进入的元素,然后删除队列中的该元素
string str1= name.Dequeue ( ); //使用peek 出队列,只会讲最下方的元素获取,不会将元素从队列中删除
string str2=name.Peek ( ); //清空队列
name.Clear ( );
}
}
}
最新文章
- 从C#到Objective-C,循序渐进学习苹果开发(2)--Objective-C和C#的差异
- ls 只显示目录
- angular的canvas画图例子
- hadoop spark学习笔记
- Cent OS 6 主机名设置
- hibernate id生成器配置
- WCF 新手教程二
- Ural 1046 Geometrical Dreams(解方程+计算几何)
- Oracle select into from 和 insert into select
- Android Studio 如何将包名按层级展示
- Sql Server 数据库中调用dll文件
- centos 下安装pptp (vpn) 的方法
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- 初识MySQL数据库的各种CMD命令窗口下的指令
- Numpy1
- 设置字体格式,加粗,regular,light
- org.hibernate.InvalidMappingException: Could not parse mapping document from无法创建sessionFactory
- 洛谷.2590.[ZJOI2008]树的统计(树分块)
- LeetCode 709 To Lower Case 解题报告
- 27.反射2.md
热门文章
- vue自学入门-4(vue slot)
- JS 动态改变浏览器title标题
- 将内裤穿在外面的男人(mysql)
- [Python]find_all函数 2020.2.7
- 假期学习【五】RDD编程实验四
- Java8之Stream详解
- Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your My
- [CQOI2009] 中位数 - 桶
- jsp报错java.io.IOException: Stream closed
- python面试的100题(18)