當前位置:
首頁 >
c#实现Stack
發布時間:2023/12/18
40
豆豆
?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?????}
?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?????}
源代碼:/Files/HeroBeast/Stack.rar
?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?????}
源代碼:/Files/HeroBeast/Stack.rar
轉載于:https://www.cnblogs.com/HeroBeast/archive/2008/07/15/1243724.html
總結
- 上一篇: php跨域资源共享,CORS 跨域资源共
- 下一篇: 【Arduino】使用C#实现Ardui