您的位置:首页 > 理论基础 > 数据结构算法

数据结构 —栈的创建、出、进栈

2016-06-15 00:00 423 查看
#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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: