#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#include "string.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef int Status;
typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */
/* 顺序栈结构 */
typedef struct
{
SElemType data[MAXSIZE];
int top; /* 用于栈顶指针 */
}SqStack;
Status push(SqStack *s, SElemType e)
{
if (s->top == MAXSIZE-1)
{
return ERROR;
}
s->top++;
s->data[s->top] = e;
return OK;
}
Status pop(SqStack *s, SElemType *e)
{
if (s->top == -1)
{
return ERROR;
}
*e = s->data[s->top];
s->top--;
return OK;
}
void main()
{
SqStack sq;
SElemType e = 0;
sq.top = 0;
push(&sq,1);
push(&sq,2);
push(&sq,3);
push(&sq,4);
pop(&sq,&e);
printf("%d",e);
pop(&sq,&e);
printf("%d",e);
pop(&sq,&e);
printf("%d",e);
pop(&sq,&e);
printf("%d",e);
getchar();
}
栈的顺序结构
最新推荐文章于 2022-09-13 15:40:27 发布