顺序栈基本操作

This commit is contained in:
Xu Bai
2019-06-29 22:47:17 +08:00
parent 077f6bac18
commit 0f746f8b31
3 changed files with 253 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.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 visit(SElemType c)
{
printf("%d ",c);
return OK;
}
/* 构造一个空栈S */
Status InitStack(SqStack *S)
{
/* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */
S->top=-1;
return OK;
}
/* 把S置为空栈 */
Status ClearStack(SqStack *S)
{
S->top=-1;
return OK;
}
/* 若栈S为空栈则返回TRUE否则返回FALSE */
Status StackEmpty(SqStack S)
{
if (S.top==-1)
return TRUE;
else
return FALSE;
}
/* 返回S的元素个数即栈的长度 */
int StackLength(SqStack S)
{
return S.top+1;
}
/* 若栈不空则用e返回S的栈顶元素并返回OK否则返回ERROR */
Status GetTop(SqStack S,SElemType *e)
{
if (S.top==-1)
return ERROR;
else
*e=S.data[S.top];
return OK;
}
/* 插入元素e为新的栈顶元素 */
Status Push(SqStack *S,SElemType e)
{
if(S->top == MAXSIZE -1) /* 栈满 */
{
return ERROR;
}
S->top++; /* 栈顶指针增加一 */
S->data[S->top]=e; /* 将新插入元素赋值给栈顶空间 */
return OK;
}
/* 若栈不空则删除S的栈顶元素用e返回其值并返回OK否则返回ERROR */
Status Pop(SqStack *S,SElemType *e)
{
if(S->top==-1)
return ERROR;
*e=S->data[S->top]; /* 将要删除的栈顶元素赋值给e */
S->top--; /* 栈顶指针减一 */
return OK;
}
/* 从栈底到栈顶依次对栈中每个元素显示 */
Status StackTraverse(SqStack S)
{
int i;
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));
return 0;
}

View File

@@ -0,0 +1,129 @@
/*
* @Author: Xu Bai
* @Date: 2019-06-29 22:05:11
* @LastEditors: Xu Bai
* @LastEditTime: 2019-06-29 22:46:28
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20
typedef int Status;
typedef int ElemType;
Status visit(ElemType e)
{
printf("%d ", e);
return 0;
}
typedef struct
{
ElemType data[MAXSIZE];
int top;
} SqStack;
Status InitStack(SqStack *S)
{
S->top = -1;
return OK;
}
Status ClearStack(SqStack *S)
{
S->top = -1;
return OK;
}
Status StackEmpty(SqStack S)
{
if (S.top == -1)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(SqStack S)
{
return S.top + 1;
}
Status GetTop(SqStack S, ElemType *e)
{
if (S.top == -1)
{
return ERROR;
}
else
{
*e = S.data[S.top];
}
return OK;
}
Status Push(SqStack *S, ElemType e)
{
//入栈
if (S->top == MAXSIZE - 1)
{
return ERROR;
}
S->data[++(S->top)] = e;
return OK;
}
Status Pop(SqStack *S, ElemType *e)
{
if (S->top == -1)
{
return ERROR;
}
*e = S->data[(S->top)--];
return OK;
}
Status SqStackTraverse(SqStack S)
{
int i = 0;
while (i <= S.top)
{
visit(S.data[i++]);
}
printf("\n");
return OK;
}
int main()
{
SqStack S;
InitStack(&S);
int j;
ElemType e;
printf("Push 0~10! \n");
for (j = 0; j < 11; j++)
{
Push(&S, j);
}
SqStackTraverse(S);
printf("Pop! \n");
Pop(&S, &e);
SqStackTraverse(S);
GetTop(S, &e);
printf("GetTop: %d \n", e);
ClearStack(&S);
printf("clear!");
SqStackTraverse(S);
getchar();
return OK;
}

BIN
a.out

Binary file not shown.