顺序栈的置空入栈出栈和返回栈顶元素

#include "stdio.h"
#include "stdlib.h"
#define MAXSIZE 100
#define OK 1
#define FALSE 0
//顺序栈结构体
typedef struct stack{
int data[MAXSIZE];
int top;
}*pstack, stack;


//置空顺序栈
int empty(pstack sq){
sq->top = -1;
printf("顺序栈已经置空!\n");
return OK;
}


//入栈操作
int push(pstack sq){
int x;
printf("请输入你想要入栈的数据:\n");
scanf("%d",&x);
sq->top = ++sq->top;
sq->data[sq->top]=x;

return OK;
}


//出栈操作
int pop(pstack sq){
if (sq->top == -1)
{
printf("顺序栈已经为空!\n");
return FALSE;
}
else
{
printf("出栈数据是:%d\n", sq->data[sq->top]);
sq->top = --sq->top;
return OK;
}
}


//遍历顺序栈中的所有数据
int show(pstack sq){
int i;
if (sq->top == -1)
{
printf("这是个空栈!\n");
return FALSE;
}
else
{
printf("栈中的元素为:\n");
for (i = 0; i <= sq->top; i++)
{
printf("%d ", sq->data[i]);
}
}
printf("\n");
}


//返回顺序栈栈顶元素
int getstacktop(pstack sq){
if (sq->top == -1)
{
printf("顺序栈中元素为空!\n");
return FALSE;
}
else
    {
printf("栈顶元素是:%d\n", sq->data[sq->top]);
return OK;
}
}
int main()
{
stack S;
int n;
do{
printf("*******************\n");
printf("1.置空顺序栈\n2.进栈操作\n3.出栈操作\n4.顺序输出栈中元素\n5.返回栈顶元素\n6.退出程序\n");
printf("*******************\n");
printf("请输入你的选择:\n");
scanf("%d",&n);
switch (n){
case 1:
empty(&S);
break;
case 2:
push(&S);
break;
case 3:
pop(&S);
break;
case 4:
show(&S);
break;
case 5:
getstacktop(&S);
break;
case 6:
break;
}
} while (n!=6);
return 0;
}

你可能感兴趣的:(C语言,顺序栈,插入修改和删除节点)