您的位置:首页 > 理论基础 > 数据结构算法

学习笔记------数据结构(C语言版)栈的顺序存储

2016-01-19 17:29 555 查看
//SqStack.cpp

#include "predefined.h"
#include "SqStack.h"
Status InitStack(SqStack *S)
//构造一个空栈
{
(*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)
//销毁栈S,S不再存在
{
free((*S).base);
(*S).base=(*S).top=NULL;//避免野指针
(*S).stacksize=0;
return OK;
}

Status ClearStack(SqStack *S)
//把S置为空栈
{
(*S).top=(*S).base;
return OK;
}

Status StackEmpty(SqStack S)
//若栈为空,返回TRUE
{
if(S.base==S.top) return TRUE;
else return FALSE;
}

int StackLength(SqStack S)
//返回栈S长度
{
return S.top-S.base;
}

Status GetTop(SqStack S,SElemType *e)
//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR。
{
if(StackEmpty(S)) return ERROR;
*e=*((S).top-1);
return OK;
}

Status Push(SqStack *S,SElemType e)
//插入元素e为新的栈顶元素
{
if(((*S).top-(*S).base)>=(*S).stacksize)
{
(*S).base=(SElemType *)realloc((*S).base,((*S).stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!(*S).base) exit(OVERFLOW);
(*S).top=(*S).base+(*S).stacksize;
(*S).stacksize+=STACKINCREMENT;
}
(*(*S).top)=e;
(*S).top++;
return OK;
}

Status Pop(SqStack *S,SElemType *e)
//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR.
{
if((*S).base==(*S).top) return ERROR;
*e=*(--(*S).top);
return OK;
}

Status StackTraverse(SqStack S,void (*visit)(SElemType *p))
//从栈底到栈顶依次对每个元素调用函数visit()。一旦visit调用失败,则操作失败
{
SElemType *p;
p=S.base;
while(p!=S.top)
{
visit(p);
p++;
}
return OK;
}


//main.cpp

#include "predefined.h"
#include "SqStack.h"
void PrintElem(SElemType *p)
{
printf("%d ",*p);
}

int main()
{
SqStack S;
Status s;
int i,n;
SElemType m;
printf("Function 1\n★函数Status InitStack(SqStack *S)测试...\n");
s=InitStack(&S);
printf("▲初始化顺序栈S: %d (0:失败 1:成功)\n\n",s);
printf("Function 2\n★函数StackEmpty(SqStack S)测试...\n");
s=StackEmpty(S);
printf("▲S是否为空栈: %d   (0:否 1:是)\n\n",s);
printf("Function 3\n★函数Push(SqStack *S,SElemType e)测试...\n");
printf("▲请输入准备向S入栈的个数:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("▲请输入La第%d个数:",i);
scanf("%d",&m);
s=Push(&S,m);
printf("▲入栈成功?:%d   (1:成功 0:失败)\n",s);
}
printf("\n");
printf("Function 4\n★函数StackTraverse(SqStack S,void (*visit)(SElemType *p))测试...\n");
printf("▲栈S中的元素为:S={");
StackTraverse(S,PrintElem);
printf("}\n\n");
printf("Function 5\n★函数Pop(SqStack *S,SElemType *e)测试...\n");
Pop(&S,&m);
printf("▲S栈顶元素\"%d\"出栈...\n",m);
printf("▲栈S中的元素为:S={");
StackTraverse(S,PrintElem);
printf("}\n\n");
printf("Function 6\n★函数StackLength(SqStack S)测试...\n");
n=StackLength(S);
printf("▲栈S的长度为:%d\n\n",n);
printf("Function 7\n★函数GetTop(SqStack S,SElemType *e)测试...\n");
GetTop(S,&m);
printf("▲S的栈顶元素为:%d\n\n",m);
printf("Function 8\n★函数ClearStack(SqStack *S)测试...\n");
printf("▲栈S置空前 :");
StackEmpty(S)?printf("空 栈!\n"):printf("非 空!\n");
ClearStack(&S);
printf("▲栈S置空前 :");
StackEmpty(S)?printf("空 栈!\n\n"):printf("非 空!\n\n");
printf("Function 9\n★函数DestroyStack(SqStack *S)测试...\n");
printf("▲栈S销毁前 :");
S.base?printf("存  在!\n"):printf("不存在!\n");
DestroyStack(&S);
printf("▲栈S销毁前 :");
S.base?printf("存  在!\n"):printf("不存在!\n");
}


//SqStack.h

//-----栈的顺序存储表示------
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef  int SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

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,void (*visit)(SElemType *p));

void PrintElem(SElemType *p);


//predefined.h

#include "stdio.h"
#include "stdlib.h"
#define   TRUE           1
#define   FALSE          0
#define   OK             1
#define   ERROR          0
#define   INFEASIBLE    -1
#define   OVERFLOW      -2
typedef   int Status;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 数据结构