转:stack
数据结构C#版笔记--堆栈(Stack)
堆栈(Stack)最明显的特征就是“先进后出”,本质上讲堆栈也是一种线性结构,符合线性结构的基本特点:即每个节点有且只有一个前驱节点和一个后续节点。
相对前面学习过的顺序表、链表不同的地方在于:Stack把所有操作限制在"只能在线性结构的某一端"进行,而不能在中间插入或删除元素。下面是示意图:

从示意图中可以看出,堆栈有二种实现方式:基于数组的顺序堆栈实现、类似链表的链式堆栈实现
先抽象堆栈的接口IStack:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
namespace 栈与队列{ public interface IStack<T> { /// <summary> /// 返回堆栈的实际元素个数 /// </summary> /// <returns></returns> int Count(); /// <summary> /// 判断堆栈是否为空 /// </summary> /// <returns></returns> bool IsEmpty(); /// <summary> /// 清空堆栈里的元素 /// </summary> void Clear(); /// <summary> /// 入栈:将元素压入堆栈中 /// </summary> /// <param name="item"></param> void Push(T item); /// <summary> /// 出栈:从堆栈顶取一个元素,并从堆栈中删除 /// </summary> /// <returns></returns> T Pop(); /// <summary> /// 取堆栈顶部的元素(但不删除) /// </summary> /// <returns></returns> T Peek(); }} |
顺序堆栈(SeqStack)的实现:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
using System;using System.Text;namespace 栈与队列{ public class SeqStack<T>:IStack<T> { private int maxsize; private T[] data; private int top; public SeqStack(int size) { data = new T[size]; maxsize = size; top = -1; } #region //接口实现部分 public int Count() { return top + 1; } public void Clear() { top = -1; } public bool IsEmpty() { return top == -1; } public void Push(T item) { if (IsFull()) { Console.WriteLine("Stack is full"); return; } data[++top] = item; } public T Pop() { T tmp = default(T); if (IsEmpty()) { Console.WriteLine("Stack is empty"); return tmp; } tmp = data[top]; top--; return tmp; } public T Peek() { if (IsEmpty()) { Console.WriteLine("Stack is empty!"); return default(T); } return data[top]; } #endregion public bool IsFull() { return top == maxsize - 1; } public override string ToString() { StringBuilder sb = new StringBuilder(); for (int i = top;i>=0;i--) { sb.Append(data[i] + ","); } return sb.ToString().Trim(','); } }} |
链式堆栈(LinkStack)的实现
先定义节点Node.cs
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
namespace 栈与队列{ public class Node<T> { private T data; private Node<T> next; public Node(T data, Node<T> next) { this.data = data; this.next = next; } public Node(Node<T> next) { this.next = next; this.data = default(T); } public Node(T data) { this.data = data; this.next = null; } public Node() { this.data = default(T); this.next = null; } public T Data { get { return this.data; } set { this.data = value; } } public Node<T> Next { get { return next; } set { next = value; } } }} |
下面是LinkStack.cs
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
using System;using System.Text;namespace 栈与队列{ public class LinkStack<T>:IStack<T> { private Node<T> top; private int num;//节点个数 /// <summary> /// 顶部节点 /// </summary> public Node<T> Top { get { return top; } set { top = value; } } public LinkStack() { top = null; num = 0; } public int Count() { return num; } public void Clear() { top = null; num = 0; } public bool IsEmpty() { if (top == null && num == 0) { return true; } else { return false; } } public void Push(T item) { Node<T> q = new Node<T>(item); if (top == null) { top = q; } else { q.Next = top; top = q; } num++; } public T Pop() { if (IsEmpty()) { Console.WriteLine("Stack is empty!"); return default(T); } Node<T> p = top; top = top.Next; num--; return p.Data; } public T Peek() { if (IsEmpty()) { Console.WriteLine("Stack is empty!"); return default(T); } return top.Data; } public override string ToString() { StringBuilder sb = new StringBuilder(); if (top != null) { sb.Append(top.Data.ToString() + ","); Node<T> p = top; while (p.Next != null) { sb.Append(p.Next.Data.ToString()+ ","); p = p.Next; } } return sb.ToString(); } }} |
测试代码片段:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
Console.WriteLine("顺序堆栈测试开始...");SeqStack<int> seqStack = new SeqStack<int>(10);seqStack.Push(1);seqStack.Push(2);seqStack.Push(3);Console.WriteLine(seqStack);Console.WriteLine(seqStack.Peek());Console.WriteLine(seqStack);Console.WriteLine(seqStack.Pop());Console.WriteLine(seqStack);Console.WriteLine("链堆栈测试开始...");LinkStack<int> linkStack = new LinkStack<int>();linkStack.Push(1);linkStack.Push(2);linkStack.Push(3);Console.WriteLine(linkStack);Console.WriteLine(linkStack.Peek());Console.WriteLine(linkStack);Console.WriteLine(linkStack.Pop());Console.WriteLine(linkStack);Console.ReadLine(); |
.Net中System.Collections.Generic.Stack<T>已经提供了堆栈的基本实现,明白原理后,仍然推荐大家使用内置的实现。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
转:stack的更多相关文章
- 线性数据结构之栈——Stack
Linear data structures linear structures can be thought of as having two ends, whose items are order ...
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder
Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
- Stack操作,栈的操作。
栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Stack的三种含义
作者: 阮一峰 日期: 2013年11月29日 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词 ...
- Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)
写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...
随机推荐
- 对动态加载javascript脚本的研究
有时我们需要在javascript脚本中创建js文件,那么在javascript脚本中创建的js文件又是如何执行的呢?和我们直接在HTML页面种写一个script标签的效果是一样的吗?(关于页面scr ...
- spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别
spring 获取 properties的值方法 在spring.xml中配置 很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx} 必须交给Dispatche ...
- [转]Magento on Steroids – Best practice for highest performance
本文转自:https://www.mgt-commerce.com/blog/magento-on-steroids-best-practice-for-highest-performance/ Th ...
- 获取ip 笔记
使用命令 Request.UserHostAddress;//IP地址 Request.UserHostName;//用户名. 1. 在ASP.NET中专用属性: 获取服务器电脑名:Page.Serv ...
- JS DOM操作(五) Window.docunment对象——操作元素
定位: var a = document.getElementByIt( "id" ) 同辈元素 var b = a.nextSibling; -- 找 a ...
- 解决jquery绑定click事件出现点击一次执行两次问题
问题定位:通过浏览器F12定位到点击一次出现两次调用. 问题复现: $("#mail_span").on("click",function(){ ...
- [android] 手机卫士号码归属地查询
使用小米号码归属地数据库,有两张表data1和data2 先查询data1表,把手机号码截取前7位 select outkey from data1 where id=”前七位手机号” 再查询data ...
- 撩课-Web大前端每天5道面试题-Day28
1.用setTimeout()方法来模拟setInterval()与setInterval()之间的什么区别? 首先来看setInterval的缺陷,使用setInterval()创建的定时器确保了定 ...
- 使用 RabbitMQ 实现异步调用
目录 引言 启动 RabbitMQ 服务器 运行 rabbitmq 容器 RabbitMQ 控制台 Exchange 和 Queue 开发服务端和客户端 开发服务端 开发客户端 Java Bean 类 ...
- BestCoder Round #27
Jump and Jump... Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...