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

 时间:2026-02-19 06:48:29

1、一、要求

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

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

2、二、代码

#include<stdio.h>

 #include<malloc.h>

 #define MAX 20

 typedef struct node

 {

 int data[MAX];

 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");

 else

 printf("栈非空!\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万的平方
  • 热门搜索
    生日快乐日语怎么写 亲昵的近义词 圆脸适合什么发型 优酷视频怎么下载 怎么建立邮箱 行距怎么设置 古装发型教程 头发发型 美国大都会博物馆 冷酷的近义词