栈定义:
是限定仅在表尾进行插入或删除操作的线性表。
栈的头尾端有特殊含意,表头称为栈顶(top),表尾称作栈底(bottom),不含元素的栈叫做空栈。
特点:后进先出。先进入的元素在栈底,后面的元素一个个压入栈中,同时也越来越接近栈顶。
常用函数:
- InitStack(&S)构造空栈
- GetTop(&S)获取栈顶元素
- Push(&S)向栈中压入元素
- Pop(&S)从栈中弹出元素
结构体
typedef struct {
int *base;
int *top;
int stacksize;
}Stack, *SqStack;
实现函数:
构造空栈
//构造栈
Status InitStack(SqStack S)
{
S->base = (int *)malloc(MAXSIZE *sizeof(int));
if (!S->base) exit(OVERFLOW);
S->top = S->base;
S->stacksize = MAXSIZE;
return OK;
}
获取栈顶元素
//获取栈顶元素
Status GetTop(SqStack S, int e)
{
if(S->top == S->base ) return ERROR;
e = *(S->top-1);//不改变指针
return e;
}
压入元素
//压入元素
Status Push(SqStack S, int e)
{
if(S->top - S->base >= S->stacksize) printf("栈满\n");
*S->top++ = e;
return OK;
}
压入元素,判断是否栈满(头尾相减与size作比较)
然后先赋值,再指针上移。
弹出元素
//弹出元素
Status Pop(SqStack S, int* e)
{
if(S->top == S->base)
{
printf("空栈\n");
exit(OVERFLOW);
}
*e = *--S->top;
return OK;
}
弹出元素应先判断是否空栈(两指针是否相等)
先指针下移,再赋值。
调用Push函数
//调用压入元素函数
void Push_Stack(SqStack S)
{
int n, flag, num;
printf("请输入你想压入的元素个数:\n");
scanf("%d", &n);
printf("请输入你想压入的元素:\n");
for (int i=0; i<n; i++)
{
scanf("%d", &num);
flag = Push(S,num);
if (flag) printf("已入栈!\n");
else printf("已满栈!\n");
}
}
调用Pop函数
//调用弹出元素函数
void Pop_Stack(SqStack S)
{
int n, num, flag;
int e;
printf("请输入你想出栈的元素个数:\n");
scanf("%d", &n);
for (int i=0; i<n; i++)
{
flag = Pop(S, &e);//下面要用到e,e应该把地址传过去,不然只是在函数内部的另一个变量。
if(flag) printf("%d已出栈\n", e);
else printf("栈已空\n");
}
}
通过返回的1或0来判断是否可以调用函数。
调用GetTop函数
//调用获取栈顶元素函数
void GetTop_Stack(SqStack S)
{
int n, e;
printf("栈顶元素为%d\n", GetTop(S,e));
}
主函数
void menu()
{
printf("********1.入栈 2.出栈*********\n");
printf("********3.取栈顶 4.退出*********\n");
}
int main()
{
int n;
SqStack S;
InitStack(S);
while (1)
{
menu();
scanf("%d", &n);
switch(n)
{
case 1: Push_Stack(S); break;
case 2: Pop_Stack(S); break;
case 3: GetTop_Stack(S); break;
case 4: exit(OVERFLOW);
}
}
return 0;
}
完整代码
#include <Stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define ERROR 0
#define OVERFLOW -1
#define Status int
#define OK 1
typedef struct {
int *base;
int *top;
int stacksize;
}Stack, *SqStack;
//构造栈
Status InitStack(SqStack S)
{
S->base = (int *)malloc(MAXSIZE *sizeof(int));
if (!S->base) exit(OVERFLOW);
S->top = S->base;
S->stacksize = MAXSIZE;
return OK;
}
//获取栈顶元素
Status GetTop(SqStack S, int e)
{
if(S->top == S->base ) return ERROR;
e = *(S->top-1);//不改变指针
return e;
}
//压入元素
Status Push(SqStack S, int e)
{
if(S->top - S->base >= S->stacksize) printf("栈满\n");
*S->top++ = e;
return OK;
}
//弹出元素
Status Pop(SqStack S, int* e)
{
if(S->top == S->base)
{
printf("空栈\n");
exit(OVERFLOW);
}
*e = *--S->top;
return OK;
}
//调用压入元素函数
void Push_Stack(SqStack S)
{
int n, flag, num;
printf("请输入你想压入的元素个数:\n");
scanf("%d", &n);
printf("请输入你想压入的元素:\n");
for (int i=0; i<n; i++)
{
scanf("%d", &num);
flag = Push(S,num);
if (flag) printf("%d已入栈!\n",num);
else printf("已满栈!\n");
}
}
//调用弹出元素函数
void Pop_Stack(SqStack S)
{
int n, num, flag;
int e;
printf("请输入你想出栈的元素个数:\n");
scanf("%d", &n);
for (int i=0; i<n; i++)
{
flag = Pop(S, &e);
if(flag) printf("%d已出栈\n", e);
else printf("栈已空\n");
}
}
//调用获取栈顶元素函数
void GetTop_Stack(SqStack S)
{
int n, e;
printf("栈顶元素为%d\n", GetTop(S,e));
}
void menu()
{
printf("********1.入栈 2.出栈*********\n");
printf("********3.取栈顶 4.退出*********\n");
}
int main()
{
int n;
SqStack S;
InitStack(S);
while (1)
{
menu();
scanf("%d", &n);
switch(n)
{
case 1: Push_Stack(S); break;
case 2: Pop_Stack(S); break;
case 3: GetTop_Stack(S); break;
case 4: exit(OVERFLOW);
}
}
return 0;
}