#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
///创建栈
typedef int data;
typedef struct
{
data *base; //栈底
data *top; //栈顶
int stacksize; //栈大小
}stack;
//创建栈(初始化)
void init(stack *s)
{
//给base开辟内存
s->base = (stack*)malloc(maxsize*sizeof(stack));
if(!s->base)
{
printf("error!");
exit(1);
}
s->top = s->base;
s->stacksize=maxsize;
}
//判断空栈 空/1
int null(stack *s)
{
return s->top == s->base;
}
//判断满栈
int full(stack *s)
{
return s->top - s->base == maxsize;
}
//进栈
void inpush(stack *s,data x)
{
if((full(s)))
{
printf("full"); //上溢
exit(1);
}
//将栈顶指针加一后再入栈;
*((s->top)++) = x;
}
//出栈
void pop(stack *s)
{
if(null(s))
{
printf("null"); //下溢
exit(1);
}
//栈顶指针-1
--s->top;
}
//取栈顶元素
/*
data top(stack *s)
{
if(null(s))
{
printf("null");
exit(1);
}
return *(s->top-1);
}*/
//输出元素
void output(stack *s)
{
while(!null(s))
{
pop(s);
printf("%d ",*s->top);
}
}
//输入元素
void input(stack *s)
{
int i,n;
data x;
//printf("please enter index:");
//scanf("%d",&n);
printf("please input element:");
//for(i=0;i<n;i++)
for(i=1;;i++)
{
scanf("%d",&x);
inpush(s,x);
if(getchar()=='\n')
break;
}
}
int main()
{
stack p,*s=&p;
init(s);//创建
input(s); //输入进栈内容
output(s); //输出出栈内容
return 0;
}
#include <stdlib.h>
#define maxsize 100
///创建栈
typedef int data;
typedef struct
{
data *base; //栈底
data *top; //栈顶
int stacksize; //栈大小
}stack;
//创建栈(初始化)
void init(stack *s)
{
//给base开辟内存
s->base = (stack*)malloc(maxsize*sizeof(stack));
if(!s->base)
{
printf("error!");
exit(1);
}
s->top = s->base;
s->stacksize=maxsize;
}
//判断空栈 空/1
int null(stack *s)
{
return s->top == s->base;
}
//判断满栈
int full(stack *s)
{
return s->top - s->base == maxsize;
}
//进栈
void inpush(stack *s,data x)
{
if((full(s)))
{
printf("full"); //上溢
exit(1);
}
//将栈顶指针加一后再入栈;
*((s->top)++) = x;
}
//出栈
void pop(stack *s)
{
if(null(s))
{
printf("null"); //下溢
exit(1);
}
//栈顶指针-1
--s->top;
}
//取栈顶元素
/*
data top(stack *s)
{
if(null(s))
{
printf("null");
exit(1);
}
return *(s->top-1);
}*/
//输出元素
void output(stack *s)
{
while(!null(s))
{
pop(s);
printf("%d ",*s->top);
}
}
//输入元素
void input(stack *s)
{
int i,n;
data x;
//printf("please enter index:");
//scanf("%d",&n);
printf("please input element:");
//for(i=0;i<n;i++)
for(i=1;;i++)
{
scanf("%d",&x);
inpush(s,x);
if(getchar()=='\n')
break;
}
}
int main()
{
stack p,*s=&p;
init(s);//创建
input(s); //输入进栈内容
output(s); //输出出栈内容
return 0;
}