C#实现自定义栈

使用C#自定义栈

1.定义一个MyStack泛型类

1using System; 2using System.Collections; 3using System.Collections.Generic; 4using System.Linq; 5using System.Text; 6using System.Threading.Tasks; 7 8namespace _06_MyStack 9{ 10 class MyStack<T> : IEnumerable<T>, IDisposable 11 { 12 //栈顶指针 13 private int _top = 0; 14 //栈的大小 15 private int _size = 0; 16 //栈的数据 17 private T[] _stack = null; 18 19 public int Top 20 { 21 get { return _top; } 22 } 23 24 public int Size 25 { 26 get { return _size; } 27 } 28 29 public int Length 30 { 31 get { return _top; } 32 } 33 34 public T this[int index] 35 { 36 get { return _stack[index]; } 37 } 38 39 /// <summary> 40 /// 栈的构造函数 41 /// </summary> 42 /// <param name="size">初始化栈的大小</param> 43 public MyStack(int size) 44 { 45 _size = size; 46 _top = 0; 47 _stack = new T[size]; 48 } 49 50 /// <summary> 51 /// 栈是否为空 52 /// </summary> 53 /// <returns></returns> 54 public bool IsEmpty() 55 { 56 return _top == 0; 57 } 58 59 /// <summary> 60 /// 栈是否已满 61 /// </summary> 62 /// <returns></returns> 63 public bool IsFull() 64 { 65 return _top == _size; 66 } 67 68 /// <summary> 69 /// 清除栈 70 /// </summary> 71 public void Clear() 72 { 73 _top = 0; 74 } 75 76 /// <summary> 77 /// 入栈 78 /// </summary> 79 /// <param name="node"></param> 80 /// <returns></returns> 81 public bool Push(T node) 82 { 83 if(!IsFull()) 84 { 85 _stack[_top] = node; 86 _top++; 87 return true; 88 } 89 return false; 90 } 91 92 /// <summary> 93 /// 出栈 94 /// </summary> 95 /// <returns></returns> 96 public T Pop() 97 { 98 //此default关键字对于引用类型会返回空,对于数值类型会返回零 99 T node = default(T); 100 if (!IsEmpty()) 101 { 102 _top--; 103 node = _stack[_top]; 104 } 105 return node; 106 } 107 108 109 110 /// <summary> 111 /// 释放资源 112 /// </summary> 113 public void Dispose() 114 { 115 _stack = null; 116 } 117 118 /// <summary> 119 /// 迭代器,用于foreach迭代 120 /// </summary> 121 /// <returns></returns> 122 public IEnumerator<T> GetEnumerator() 123 { 124 for(int i=0;i<_stack.Length;i++) 125 { 126 if(_stack[i] != null) 127 { 128 yield return _stack[i]; 129 } 130 } 131 } 132 133 IEnumerator IEnumerable.GetEnumerator() 134 { 135 return GetEnumerator(); 136 } 137 } 138}

2.测试

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6 7/// <summary> 8/// 06_MyStack 9/// 自定义实现栈 10/// 栈特点:先进后出 11/// </summary> 12namespace _06_MyStack 13{ 14 class Person 15 { 16 public string Name { get; set; } 17 public int Age { get; set; } 18 } 19 20 class Program 21 { 22 static void Main(string[] args) 23 { 24 Person p1 = new Person 25 { 26 Name = "Flyee", 27 Age = 24 28 }; 29 Person p2 = new Person 30 { 31 Name = "Flyee2", 32 Age = 18 33 }; 34 Stack<Person> stack = new Stack<Person>(2); 35 36 MyStack<Person> myStack = new MyStack<Person>(2); 37 myStack.Push(p1); 38 myStack.Push(p2); 39 40 foreach(var p in myStack) 41 { 42 Console.WriteLine(p.Name); 43 } 44 45 Console.WriteLine(myStack[1].Age); 46 47 Person result = myStack.Pop(); 48 Console.WriteLine(myStack.Length); 49 Console.WriteLine("Name:" + result.Name + " Age:" + result.Age); 50 myStack.Pop(); 51 Console.WriteLine(myStack.Length); 52 myStack.Dispose(); 53 54 Console.Read(); 55 } 56 } 57}

3.测试结果

参考博客地址:https://www.cnblogs.com/yezhu008/p/5726234.html

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Java获得今日零时零分零秒的时间(Date型)

publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS