#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#define N 100
struct mystack
{
int top;
int data[N];//存储栈上的数据
};
void init(struct mystack *p)//初始化
{
p->top = -1;//-1:代表空栈
memset(p->data, 0, sizeof(int)*N);//清空内存
}
void clear(struct mystack *p)//清空
{
init(p);
}
int isempty(struct mystack *p)
{
if (p->top == -1)
{
return 1;//1:代表空
}
else
{
return 0;//不为空
}
}
int isfull(struct mystack *p)
{
if (p->top == N-1)
{
return 1;//满了
}
else
{
return 0;
}
}
int gettop(struct mystack *p)
{
return p->data[p->top];//获取即将出栈的值
}
void push(struct mystack *p,int key)
{
if (isfull(p)==1)
{
return;
}
else
{
p->top++;//下标移动一个
p->data[p->top] = key;//压栈
}
}
void pop(struct mystack *p)
{
if (isempty(p)==1)
{
return;
}
else
{
p->top--;
}
}
void main()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
struct mystack mystackX;//定义结构体对象
init(&mystackX);
for (int i = 0; i < 10; i++)
{
push(&mystackX, a[i]);
}
while(!isempty(&mystackX))
{
printf("%d\n", gettop(&mystackX));
pop(&mystackX);
}
getchar();
}