数据结构——栈的数组实现
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 5
typedef struct Stack
{
int Array[MaxSize];
int top;
}Stack;
Stack* CreateStack()
{
Stack* s;
s = (Stack*)malloc(sizeof(Stack));
if (s == NULL)
printf("分配内存空间失败!");
s->top = 0;
return s;
}
Stack* Push(int X, Stack* s)
{
s->Array[s->top] = X;
s->top++;
return s;
}
Stack* Pop(Stack* s)
{
if (s->top == 0)
printf("该栈是空栈!");
else
s->top--;
}
void Display(Stack* s)
{
int p;
p = s->top;
while (p != 0)
{
printf("%d", s->Array[p-1]);
p--;
}
printf("\n");
}
void main()
{
int i;
Stack* s;
s = CreateStack();
for (i = 0; i < MaxSize; i++)
{
s = Push(i, s);
Display(s);
}
s = Pop(s);
Display(s);
}