栈
抽象数据类型
ADT 栈(stack)
Data
同线性表。元素具有相同的类型,相邻元素具有前驱和后驱的关系。
Operation
InitStack(*S) : 初始化操作,建立一个空栈S
DestroyStack(*S) : 若栈存在,就销毁它
ClearStack(*S) : 将栈清空
StackEmpty(S) : 若栈为空,则返回true
GetTop(S,*e) : 若栈存在且非空,用e返回S的栈顶元素
Push(*S, e) : 若栈S存在,插入新元素e到栈S中并成为栈顶元素
Pop(*S, *e) : 删除栈S中栈顶元素,并用e返回其值
StackLength(S) : 返回栈S中的元素个数
endADT
顺序存储结构
#include <stdio.h>
#define MAXSIZE 100
#define True 1
#define False 0
typedef int Status;
typedef int SElemType;
typedef struct
{
SElemType *Data;
int top;
int stacklength;
} SqStack;
Status InitStack(SqStack *S)
{
S->Data = (SElemType *)malloc(MAXSIZE * sizeof(SElemType));
S->top = -1;
S->stacklength = MAXSIZE;
return True;
}
Status DestroyStack(SqStack *S)
{
if (S->Data)
{
free(S->Data);
S->top = -1;
S->Data = NULL;
S->stacklength = 0;
return True;
}
else
return False;
}
Status ClearStack(SqStack *S)
{
S->top = -1;
return True;
}
Status StackEmpty(SqStack S)
{
if (S.top == -1)
return True;
else
return False;
}
Status GetTop(SqStack S, SElemType *e)
{
if (S.Data && S.top != -1)
{
*e = S.Data[S.top];
return True;
}
return False;
}
Status Push(SqStack *S, SElemType e)
{
if (!S->Data || S->top == MAXSIZE - 1)
return False;
++S->top;
S->Data[S->top] = e;
return True;
}
Status Pop(SqStack *S, SElemType *e)
{
if (!S->Data || S->top == -1)
return False;
*e = S->Data[S->top];
--S->top;
return True;
}
int StackLength(SqStack S)
{
return S.top + 1;
}
两栈共享空间
#include <stdio.h>
#define MAXSIZE 100
#define True 1
#define False 0
typedef int Status;
typedef int SElemType;
typedef struct
{
SElemType *Data;
int top1;
int top2;
} SqDoubleStack;
Status InitStack(SqDoubleStack *S)
{
S->Data = (SElemType *)malloc(MAXSIZE * sizeof(SElemType));
S->top1 = -1;
S->top2 = MAXSIZE;
return True;
}
Status DestroyStack(SqDoubleStack *S)
{
if (S->Data)
{
free(S->Data);
S->Data = NULL;
S->top1 = -1;
S->top2 = MAXSIZE;
return True;
}
return False;
}
Status ClearStack(SqDoubleStack *S)
{
S->top1 = -1;
S->top2 = MAXSIZE;
return True;
}
Status StackEmpty(SqDoubleStack S)
{
if (S.top1 == -1 && S.top2 == MAXSIZE)
return True;
return False;
}
Status GetTop(SqDoubleStack S, SElemType *e, int flag)
{
if (flag == 1)
{
*e = S.Data[S.top1];
return True;
}
if (flag == 2)
{
*e = S.Data[S.top2];
return True;
}
return False;
}
Status Push(SqDoubleStack *S, SElemType e, int flag)
{
if (S->top1 + 1 == S->top2)
return False;
if (flag == 1)
{
++S->top1;
S->Data[S->top1] = e;
}
if (flag == 2)
{
++S->top2;
S->Data[S->top2] = e;
}
return False;
}
Status Pop(SqDoubleStack *S, SElemType *e, int flag)
{
if (S->top1 != -1 && flag == 1)
{
*e = S->Data[S->top1];
--S->top1;
return True;
}
if (S->top2 != MAXSIZE && flag == 2)
{
*e = S->Data[S->top2];
++S->top2;
return True;
}
return False;
}
int StackLenth(SqDoubleStack S)
{
int i = 0;
while (S.Data[i])
++i;
return i;
}
链式存储结构
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define True 1
#define False 0
typedef int Status;
typedef int SElemType;
typedef struct StackNode
{
SElemType data;
struct StackNode *next;
} StackNode, *LinkStackPtr;
typedef struct LinkStack
{
LinkStackPtr top;
int conut;
} LinkStack, *LinkStackPoint;
Status InitStack(LinkStackPoint *S)
{
*S = (LinkStackPoint)malloc(sizeof(LinkStack));
(*S)->top = (LinkStackPtr)malloc(sizeof(StackNode));
(*S)->top = NULL;
(*S)->conut = 0;
return True;
}
Status ClearStack(LinkStackPoint S)
{
LinkStackPtr p;
while (S->top)
{
p = S->top;
S->top = p->next;
free(p);
--S->conut;
}
S->top = NULL;
return True;
}
Status DestroyStack(LinkStackPoint *S)
{
ClearStack(*S);
free(*S);
*S = NULL;
return True;
}
Status StackEmpty(LinkStack S)
{
if (S.conut == 0)
return True;
return False;
}
Status GetTop(LinkStackPoint S, SElemType *e)
{
if (S->conut == 0)
return False;
*e = S->top->data;
return True;
}
Status Push(LinkStackPoint S, SElemType e)
{
if (!S)
return False;
LinkStackPtr N = (LinkStackPtr)malloc(sizeof(StackNode));
N->next = S->top;
S->top = N;
S->top->data = e;
++S->conut;
return True;
}
Status Pop(LinkStackPoint S, SElemType *e)
{
LinkStackPtr p;
p = S->top;
if (S->conut == 0)
return False;
*e = S->top->data;
S->top = p->next;
free(p);
--S->conut;
return True;
}
int StackLength(LinkStack S)
{
return S.conut;
}