#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct node Stack;
const int Max = 10;
struct node
{
int top;
int array[Max];
};
//创建
void creat(Stack *s)
{
s->top = 0;
}
//判断是否为空
int IsEmpty(Stack *s)
{
return s->top;
}
//进栈
void push(int x, Stack *s)
{
if (Max <= s->top + 1) {
printf("Out of space!\n");
return;
}
s->array[s->top] = x;
++s->top;
}
//出栈
void pop(Stack *s)
{
if (!IsEmpty(s)) {
printf("Empty\n");
return;
}
printf("已出栈!\n");
--s->top;
}
//返回栈顶
int top(Stack *s)
{
if (!IsEmpty(s)) {
printf("Empty!");
return 0;
}
return s->array[s->top - 1];
}
//遍历输出
void display(Stack *s)
{
int temp = s->top;
--temp;
printf("遍历输出:");
while (temp >= 0) {
printf("%d ", s->array[temp]);
--temp;
}
printf("\n");
}
int main()
{
Stack s;
creat(&s);
push(45, &s);
push(5, &s);
push(15, &s);
push(100, &s);
push(51, &s);
display(&s);
printf("栈顶:%d\n", top(&s));
pop(&s);
display(&s);
printf("栈顶:%d\n", top(&s));
return 0;
}
07-28
07-28
07-28