一、栈
定义:一中特殊的线性表,其插入和收纳处均在表的一端进行,是一种运算受限的线 性表。其中,允许插入和删除的一端称之为栈顶,另一端为栈底。
栈的特点——后进先出
栈有两种表示方法,一种是顺序表实现栈,另一种是链式存储实现栈。这里主要介绍链式存储实现栈的数据结构。
a).栈的顺序存储(简介)
即定义一个数组,以数组头为栈底,设一个int top指针指向栈顶,即可完成栈的基本操作。
b)栈的链式存储
1.栈的结构定义——其中,top为栈顶,buttom为栈底
typedef struct node
{
int data;
struct node*next;
}node,*pnode;//define node
typedef struct stack
{
pnode top;
pnode buttom;
}stack,*pstack;//define stack
2.栈的初始化——初始化的时候将栈的栈顶指针指向栈底指针。
pstack CreateStack()
{
pstack st;
st=(pstack)malloc(sizeof(stack));
assert(st);
st->top=st->buttom;
return st;
}//创建栈的函数
3.栈的摧毁
void DestroyStack(pstack st)
{
pnode n;
while(st->top!=st->buttom)
{
n=st->top;
st->top