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

 时间: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、三、运行结果

编程实现栈的顺序存储和基本操作
  • C#如何使用逐字字符串
  • 如何提高Java水平
  • Win10在哪更改鼠标触摸回应
  • 利用Spring编写Dao组件的方法(一)
  • Python怎么计算10万的平方
  • 热门搜索
    贝肯鲍尔踢什么位置 周末祝福语 天枰座和什么星座最配 抽烟机什么牌子的好 5w1h是什么意思 别具匠心是什么意思 资本化是什么意思 什么可以去斑 粉墨登场是什么意思 男朋友生日祝福语