参考书籍:《大话数据结构》
环境:VS2017
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 0
#define FALSE 0
#define MAXSIZE 20
typedef int Status;
typedef int SElemType;
Status visit(SElemType e)
{
printf("%d ", e);
return OK;
}
typedef struct
{
SElemType data[MAXSIZE];
int top;
}SqStack;
Status InitStack(SqStack *S)
{
S->top = -1;
return OK;
}
Status StackEmpty(SqStack S)
{
if (S.top == -1)
return TRUE;
else
return FALSE;
}
Status ClearStack(SqStack *S)
{
S->top = -1;
return OK;
}
int StackLength(SqStack S)
{
return S.top + 1;
}
Status Push(SqStack *S, SElemType e)
{
if (S->top == MAXSIZE - 1)
return ERROR;
S->top++;
S->data[S->top] = e;
return OK;
}
Status GetTop(SqStack S, SElemType *e)
{
if (S.top == -1)
return ERROR;
else
*e = S.data[S.top];
return OK;
}
Status Pop(SqStack *S, SElemType *e)
{
if (S->top == -1)
return ERROR;
*e = S->data[S->top];
S->top--;
return OK;
}
Status StackTraverse(SqStack S)
{
int i = 0;
while (i <= S.top)
{
visit(S.data[i++]);
}
printf("\n");
return OK;
}
int main()
{
int j;
SqStack s;
int e;
if (InitStack(&s) == OK)
for (j = 1; j <= 10; j++)
Push(&s, j);
printf("栈中元素依次为:");
StackTraverse(s);
Pop(&s, &e);
printf("弹出的栈顶元素 e=%d\n", e);
printf("栈空否:%d(1:空 0:否)\n", StackEmpty(s));
GetTop(s, &e);
printf("栈顶元素 e=%d 栈的长度为%d\n", e, StackLength(s));
ClearStack(&s);
printf("清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(s));
system("pause");
return 0;
}