转自http://blog.sina.com.cn/s/blog_1513d729e0102wem6.html
顺序栈的实现(利用数组实现)
在数据结构中,栈是一中特殊的ADT(抽象数据类型)。它的特殊性在于它的操作只能在一个位置进行,无论是入栈还是出栈。这个位置是表的末端,也叫做栈顶。栈是操作受限的线性表。栈在计算机科学中几乎无处不在,是一中非常重要的数据结构。
用数组实现栈代码简单,但是也有缺点,那就是必须提前声明一个数组的大小,并且在程序的运行过程中不能改变,这在一定程度上限制了栈的灵活性,但是却带来了更为简洁的代码。数组的下标为0我们表示栈空。
#include
#include
#define
//栈的节点声明
typedef
Stack
//初始化一个空栈
int
//测试是否为空栈
int
//元素e入栈
int
//出栈,并返回该元素值
int
void
栈的基本操作实现后,我们来看下利用栈的一个小题目。编写程序,识别以@为结束符的字符序列是否为形如‘序列1&序列2’模式的字符序列。其中序列1和2都不含‘&’字符,且序列1和序列2互为逆序列。这其实就是利用栈的Pop()操作。代码如下:
#include
#include
#define
#define
//栈的节点声明
typedef
//初始化一个空栈
int
//测试是否为空栈
int
//元素e入栈
int
//出栈,并返回该元素值
EleType
void