1.push将对象插入 System.Collections.Generic.Stack<T> 的顶部。

Stack st = new Stack(); //栈是先进后出
st.Push(1);
st.Push(2);
st.Push(3);
st.Push(4);

2.peek读栈

(1)

foreach (var item in st) //读栈的时候读的是栈的“上面”
{
Console.WriteLine(item);
}

(2)
Console.WriteLine(st.Peek());

3.Pop()移除并返回位于栈顶部的对象

foreach (var item in st) //读栈的时候读的是栈的“上面”
{
Console.WriteLine(item);
}
//Console.WriteLine(st.Peek());

Console.WriteLine("**********");
st.Pop();

foreach (var item in st) //读栈的时候读的是栈的“上面”
{
Console.WriteLine(item);

4.clear清除栈内所有内容

foreach (var item in st) //读栈的时候读的是栈的“上面”
{
Console.WriteLine(item);
}
//Console.WriteLine(st.Peek());

Console.WriteLine("**********");

st.Clear();
foreach (var item in st)
{
Console.WriteLine(item);
}

——————————————————————————————————

Queue队列  :先进先出

1.Enqueue将对象添加到Queue的结尾处

Queue qu = new Queue();
qu.Enqueue(1);
qu.Enqueue(2);
qu.Enqueue(3);
qu.Enqueue(4);

2.Peek 返回位于Queue开始处的对象但不将其移除(读队)

(1) Console.WriteLine(qu.Peek());

(2)

foreach (var item in qu)
       {
         Console.WriteLine(item);
       }
      

3.Dequeue移除并返回Queue开始处的对象

Console.WriteLine(qu.Dequeue());
Console.WriteLine("*********");
foreach (var item in qu)
{
Console.WriteLine(item);
}

4.clear清除所有元素 和 栈一样

stack栈和Queue队列的更多相关文章

  1. Java数据类型Stack栈、Queue队列、数组队列和循环队列的比较

    判断括号是否匹配:调用java本身 import java.util.Stack; public class Solution { public boolean isValid(String s){ ...

  2. 用数组模拟STL中的srack(栈)和queue(队列)

    我们在理解stack和queue的基础上可以用数组来代替这两个容器,因为STL中的stack和queue有可能会导致程序运行起来非常的慢,爆TLE,所以我们使用数组来模拟他们,不仅可以更快,还可以让代 ...

  3. C#部分---特殊集合:stack栈集合、queue队列集合、哈希表集合。

    1.stack栈集合:又名 干草堆集合 栈集合 特点:(1)一个一个赋值 一个一个取值(2)先进后出实例化 初始化 Stack st = new Stack(); //添加元素用push st.Pus ...

  4. java集合类——Stack栈类与Queue队列

    Stack继承Vector类,它通过五个操作对类 Vector 进行了扩展. 栈是 后进先出的. 栈提供了通常的 push 和 pop 操作,以及取堆栈顶点的 peek 方法.测试堆栈是否为空的 em ...

  5. Stack栈类与、Queue队列与线性表的区别和联系

    栈和队列都属于特殊的线性表   一.定义   1.线性表(linear list): 是数据结构的一种,一个线性表是n个具有相同特性的数据元素的有限序列.数据元素是一个抽象的符号,其具体含义在不同的情 ...

  6. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  7. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. Stack集合 Queue队列集合 Hashtable哈希表

    Stack集合 干草堆集合 栈集合 栈;stack,先进后出,一个一个赋值,一个一个取值,安装顺序来. 属性和方法 实例化 初始化 Stack st = new Stack(); 添加元素 个数 Co ...

  9. stack堆栈容器、queue队列容器和priority_queue优先队列容器(常用的方法对比与总结)

    stack堆栈是一个后进先出的线性表,插入和删除元素都在表的一端进行. stack堆栈的使用方法: 采用push()方法将元素入栈: 采用pop()方法将元素出栈: 采用top()方法访问栈顶元素: ...

随机推荐

  1. 第一次玩博客 感觉自己特别low

    第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动 ...

  2. shell之小知识点

    last:显示/var/log/wtmp文件,显示用户登录历史及重启历史     -n #:仅显示最近几次的相关信息 lastb:/var/log/btmp文件,显示用户错误的登录尝试     -n ...

  3. [ecmagent][redis学习][1初识redis] python操作redis

    #1 连接redis # 连接redis -- import redis -- 使用端口连接redis conn = redis.Redis(host=) -- 使用套接字连接 r = redis.R ...

  4. # Including Artificial Intelligence in a Routing ProtocolUsing Software Defined Networks

    Abstract: 问题:AI在路由协议上的应用仅适用于真实设备,尤其是无线传感器节点 The inclusion of artificial intelligence (AI) can improv ...

  5. 基于linux操作系统安装、使用memcached详解

    1.memcached的应用背景及作用 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态. ...

  6. IIS7 无法显示 htm js 图片 css的问题

    中文:开始->控件面板->程序->打开或关闭windows功能->Internet信息服务->万维网服务->常见http功能->静态内容(选中) englis ...

  7. 浅析_tmain() 与 main() 函数的区别

    _tmain()是为了支持Unicode所使用的main的一个别名,既然是别名,应该有宏定义过的,在<stdafx.h>里 #include <stdio.h> #indlud ...

  8. 【ZBH选讲·模数和】

    [问题描述]你是能看到第二题的friends呢.——laekovHja和Yjq在玩游戏,这个游戏中Hja给了Yjq两个数,希望Yjq找到一些非负整数使得这些数的和等于n,并且所有数模maaaaaaaa ...

  9. uva1214 Manhattan Wiring 插头DP

    There is a rectangular area containing n × m cells. Two cells are marked with “2”, and another two w ...

  10. OnCommand® Unified Manager

    OnCommand Unified Manager Solution Components   The following components are downloaded and installe ...