#include <stdio.h>
#include <stdlib.h>
#define maxsize 20
typedef int ElemType;
typedef struct Stack{
ElemType data[maxsize];
int top;
}Stack;
//初始化
Stack * Init()
{
Stack *s=(Stack*)malloc(sizeof(Stack));
s->top=-1;
return s;
}
//进栈
void Push(Stack *s,ElemType e)
{
if(s->top==maxsize-1)
return;
// s->data[s->top+1]=e;
// s->top++;
s->data[++(s->top)]=e;
}
//出栈
void Pop(Stack *s,ElemType *e)
{
if(s->top==-1)
*e=s->data[s->top--];
}
//输出
void out(Stack *s)
{
int i=s->top;
while(i!=-1)
{
printf("%d ",s->data[i]);
i--;
}
printf("\n");
}
//主函数
void main()
{
Stack *s;
ElemType e;
s=Init();
printf("这是栈的、请输入数据:\n");
scanf("%d",&e);
while(e!=0)
{
Push(s,e);
scanf("%d",&e);
}
out(s);
}
数据结构 —栈的创建、出、进栈
最新推荐文章于 2025-03-05 18:00:07 发布