C# 集合、字典、栈和队列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 定义可变数据组类型
List<Dog> list = new List<Dog>();
// 添加数组元素
list.Add(new Dog("A"));
list.Add(new Dog("B"));
list.Add(new Dog("C"));
list.Add(new Dog("D"));
list.Add(new Dog("E"));
list.RemoveAt(); // 删除第二个,1表示的是下标
; i < list.Count; ++i)
{
list[i].ShowName();
}
}
public class Dog
{
private string Name;
public Dog(string name)
{
Name = name;
}
public void ShowName()
{
Console.WriteLine("狗的名字是:" + Name);
}
}
}
}
字典的使用形式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 字典的定义
Dictionary<string, Dog> dog1 = new Dictionary<string, Dog>();
// 添加形式
dog1.Add("A", new Dog("A"));
dog1.Add("B", new Dog("B"));
dog1.Add("C", new Dog("C"));
dog1.Add("D", new Dog("D"));
// 访问形式
dog1["B"].ShowName();
}
public class Dog
{
private string Name;
public Dog(string name)
{
Name = name;
}
public void ShowName()
{
Console.WriteLine("狗的名字是:" + Name);
}
}
}
}
栈的使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 调用栈
Stack<Dog> stack1 = new Stack<Dog>();
// push进栈操作
stack1.Push(new Dog("A"));
stack1.Push(new Dog("B"));
stack1.Push(new Dog("C"));
// peek() 查看当前栈顶元素
stack1.Peek().ShowName(); // 显示"C"
// 弹出栈顶指针
stack1.Pop(); // 弹出"C"
stack1.Peek().ShowName(); // 此时应该显示"B",因为"C"已经被pop()弹出
}
public class Dog
{
private string Name;
public Dog(string name)
{
Name = name;
}
public void ShowName()
{
Console.WriteLine("狗的名字是:" + Name);
}
}
}
}
队列的使用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 队列的使用
Queue<Dog> dog1 = new Queue<Dog>();
// 进队操作
dog1.Enqueue(new Dog("A"));
dog1.Enqueue(new Dog("B"));
dog1.Enqueue(new Dog("C"));
// 出队列操作
dog1.Dequeue().ShowName(); // 先进先出,所以弹出的是"A"
}
public class Dog
{
private string Name;
public Dog(string name)
{
Name = name;
}
public void ShowName()
{
Console.WriteLine("狗的名字是:" + Name);
}
}
}
}
C# 集合、字典、栈和队列的更多相关文章
- 用LinkedList集合演示栈和队列的操作
在数据结构中,栈和队列是两种重要的线性数据结构.它们的主要不同在于:栈中存储的元素,是先进后出:队列中存储的元素是先进先出.我们接下来通过LinkedList集合来演示栈和队列的操作. import ...
- LinkedList集合 实现栈和队列
LinkedList集合的底层是链表结构实现的,所以可以模拟栈(先进后出)和队列(先进先出). 方法: addFirst() //添加元素到列表的起始位置 addLast() //添加元素到列表的结束 ...
- java集合详解(附栈,队列)
1 集合 1.1 为什么会出现集合框架 [1] 之前的数组作为容器时,不能自动拓容 [2] 数值在进行添加和删除操作时,需要开发者自己实现添加和删除. 1.2 Collection接口 1.2.1 C ...
- 集合第七发练习之利用ArrayList做栈、队列
栈:后进先出. 队列:先进先出. 利用这个核心思想,结合集合的add和remove方法做的栈和队列如下代码所示: 主类(t1()和t2()分别演栈和队列): package cn.hncu.myS ...
- Collection集合重难点梳理,增强for注意事项和三种遍历的应用场景,栈和队列特点,数组和链表特点,ArrayList源码解析, LinkedList-源码解析
重难点梳理 使用到的新单词: 1.collection[kəˈlekʃn] 聚集 2.empty[ˈempti] 空的 3.clear[klɪə(r)] 清除 4.iterator 迭代器 学习目标: ...
- 集合类——集合输出、栈和队列及Collections集合
1.集合输出 在之前我们利用了toString()及get()方法对集合进行了输出,其实那都不是集合的标准输出,集合输出有四种方式:Iterator.ListIterator.Enumeration. ...
- 数据结构和算法(Golang实现)(14)常见数据结构-栈和队列
栈和队列 一.栈 Stack 和队列 Queue 我们日常生活中,都需要将物品排列,或者安排事情的先后顺序.更通俗地讲,我们买东西时,人太多的情况下,我们要排队,排队也有先后顺序,有些人早了点来,排完 ...
- 学习javascript数据结构(一)——栈和队列
前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...
- 用JS描述的数据结构及算法表示——栈和队列(基础版)
前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
- 《数据结构与算法分析:C语言描述_原书第二版》CH3表、栈和队列_reading notes
表.栈和队列是最简单和最基本的三种数据结构.基本上,每一个有意义的程序都将明晰地至少使用一种这样的数据结构,比如栈在程序中总是要间接地用到,不管你在程序中是否做了声明. 本章学习重点: 理解抽象数据类 ...
随机推荐
- 32位linux(ubuntu) exec: arm-none-linux-gnueabi-g++未找到;The tslib functionality test failed!
请先参考:http://blog.csdn.net/ankwyq/article/details/7768809 通过上面那篇文章,我确实把问题又推进了一步,接下来就是下面这个问题: exec: ar ...
- python3 自学第一天,python 介绍
1.python的介绍: 是一个无聊的人创造的 2.python的格式: 跟java这些语言格式不一样用的是缩进来编码(区块) 一般是四个空格,这样更简洁 3.编码格式: python3跟python ...
- python day17面向对象-组合
组合: 给一个类的对象封装一个属性,这个属性是另一个类的对象. class GameRole: def __init__(self, name, ad, hp): self.name = name s ...
- SQL注入之Sqli-labs系列第十三关(基于单引号POST的报错注入)
开始挑战第十三关(Double Injection- String- with twist) 访问地址,输入报错语句 ' '' ') ") - 等使其报错 分析报错信息 很明显是需要采 ...
- Android动态添加Device Admin权限
/********************************************************************** * Android动态添加Device Admin权限 ...
- array的方法 没记住的
reserve() 是倒叙: sort() 拍序,按字符编码排序,可以传一个参数 reduce() 实例:判断一个数组里参数的个数 var arr = ["apple"," ...
- ACM-ICPC 2018 沈阳赛区网络预赛-K:Supreme Number
Supreme Number A prime number (or a prime) is a natural number greater than 11 that cannot be formed ...
- nginx根据url中的参数进行转发
在实际项目中,由于https安全策略,我们无法直接跳转到我们想要跳转到的地址 例如 url:https://abc.dc.com/image?url=https://vpic.video.qq.com ...
- MyBatis sql语句使用总结
MyBatis中Like语句使用总结 oracle数据库: SELECT * FROM user WHERE name like CONCAT('%',#{name},'%') 或 : SELECT ...
- hdu3605 Escape 二分图多重匹配/最大流
2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...