编程实现栈的顺序存储和基本操作

时间:2024-10-17 04:02:31

1、一、要求编程实现栈的顺序存储和基本操作(初始化、入栈、取栈顶元素、出栈等)

编程实现栈的顺序存储和基本操作

2、二、代码#include<stdio.h>#include<malloc.h>#define MAX 20typedef struct node{int data[M帆歌达缒AX];int top;}Stack;void Initial_Stack(Stack * &s)//初始化栈{s=(Stack *)malloc(sizeof(Stack));s->top=-1;}void Empty_Stack(Stack *s)//判断栈是否为空{if(-1 == s->top)printf("栈空!\n");elseprintf("栈非空!\n");}void Creat_Stack(Stack * &s)//创建栈{int i=0;printf("Enter the data:\n");do{scanf("%d",s->data+i);s->top=i;i++;}while(i<MAX&&getchar()!='\n');}void Disp_Stack(Stack * &s)//打印栈{int i;for(i=s->top;i>=0;i--)printf("%d: %d\n",s->top-i+1,s->data[i]);}void Pop_Stack(Stack * &s)//出栈{if(s->top==-1){printf("Stack empty!\n");return;}s->top--;printf("After pop:\n");Disp_Stack(s);}int main(){Stack *S;Initial_Stack(S);Empty_Stack(S);Creat_Stack(S);Empty_Stack(S);printf("Initial stack:\n");Disp_Stack(S);Pop_Stack(S);return 0;}

编程实现栈的顺序存储和基本操作

3、三、运行结果

编程实现栈的顺序存储和基本操作
© 2025 光影知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com