数据结构教程第十七课实验三:栈的表示与实现及栈的应用

文章作者 100test 发表时间 2007:03:10 18:29:18
来源 100Test.Com百考试题网


教学目的: 掌握栈的存储表示方式和栈基本操作的实现方法
教学重点: 栈的基本操作实现方法,栈的应用
教学难点: 栈的存储表示
实验内容:
一、栈的实现
实现栈的顺序存储。
栈实现示例
#include
#include
#include

#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef int Status .

struct STU{
char name[20].
char stuno[10].
int age.
int score.
}.
typedef struct STU SElemType.

struct STACK
{
SElemType *base.
SElemType *top.
int stacksize.
}.

typedef struct STACK SqStack.
typedef struct STACK *pSqstack.

Status InitStack(SqStack **S).
Status DestroyStack(SqStack *S).
Status ClearStack(SqStack *S).
Status StackEmpty(SqStack S).
int StackLength(SqStack S).
Status GetTop(SqStack S,SElemType *e).
Status Push(SqStack *S,SElemType e).
Status Pop(SqStack *S,SElemType *e).
Status StackTraverse(SqStack S,Status (*visit)()).

Status InitStack(SqStack **S)
{
(*S)=(SqStack *) malloc(sizeof(SqStack)).
(*S)->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType)).
if(!(*S)->base)exit(OVERFLOW).
(*S)->top=(*S)->base.
(*S)->stacksize=STACK_INIT_SIZE.
return OK.
}

Status DestroyStack(SqStack *S)
{
free(S->base).
free(S).
}

Status ClearStack(SqStack *S)
{
S->top=S->base.
}

Status StackEmpty(SqStack S)
{
if(S.top==S.base) return TRUE.
else
return FALSE.
}

int StackLength(SqStack S)
{
int i.
SElemType *p.
i=0.
p=S.top.
while(p!=S.base)
{p .
i .
}
}

相关文章


数据结构教程第二十课广义表
数据结构教程第十八课数组的顺序表示与实现
数据结构教程第十七课实验三:栈的表示与实现及栈的应用
数据结构教程第十六课串操作应用举例
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛