c#实现Stack
1 public class Nodes //结点类
2 {
3 public Nodes Next;
4 public object Value;
5 public Nodes(object value) : this(value, null) { }
6 public Nodes(object value, Nodes next)
7 {
8 Next = next;
9 Value = value;
10 }
11 }
2 {
3 public Nodes Next;
4 public object Value;
5 public Nodes(object value) : this(value, null) { }
6 public Nodes(object value, Nodes next)
7 {
8 Next = next;
9 Value = value;
10 }
11 }
1 public class Stack
2 {
3 private int count = 0;
4 private Nodes first = null;//定义首结点
5
6 public bool Empty
7 {
8 get{ return (first == null);}
9 }
10
11
12 public int Count
13 {
14 get{return count;}
15 }
16
17 public object Pop()//入栈
18 {
19 if (first == null)
20 {
21 throw new InvalidOperationException("Can not pop from an empty stack;");
22 }
23 else
24 {
25 object temp = first.Value;
26 first = first.Next;
27 count--;
28 return temp;
29 }
30 }
31
32 public void push(object o)//出栈
33 {
34 first = new Nodes(o, first);
35 count++;
36 }
37
38 public Stack(){ }
39
40
41
42 }
2 {
3 private int count = 0;
4 private Nodes first = null;//定义首结点
5
6 public bool Empty
7 {
8 get{ return (first == null);}
9 }
10
11
12 public int Count
13 {
14 get{return count;}
15 }
16
17 public object Pop()//入栈
18 {
19 if (first == null)
20 {
21 throw new InvalidOperationException("Can not pop from an empty stack;");
22 }
23 else
24 {
25 object temp = first.Value;
26 first = first.Next;
27 count--;
28 return temp;
29 }
30 }
31
32 public void push(object o)//出栈
33 {
34 first = new Nodes(o, first);
35 count++;
36 }
37
38 public Stack(){ }
39
40
41
42 }
源代码:/Files/HeroBeast/Stack.rar
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步