#include<stdio.h>
#include <iostream>
#include< stdlib.h>
using namespace std;
#define MAX 20
#define OK 1
#define NO 0
typedef int ElemType;/*数组储存类型*/
typedef int Status; /*函数返回值*/
typedef struct Stack {
ElemType num[MAX];
int Last;
}Stack;
typedef struct Stack* stack;
Status InitStack(stack s)
{
s->Last = 0;
return OK;
}
Status DestoryStack(stack *s)
{
free(s);
return OK;
}
Status ClearStack(stack s)
{
s->Last = 0;
return OK;
}
Status StackEmpty(stack s)
{
if (s->Last == 0)
return true;
return false;
}
Status GetTop(stack s,ElemType* e)
{
if (s->Last != 0)
{
*e = s->num[s->Last-1];
return OK;
}
return NO;
}
Status Push(stack s,int e)
{
if (s->Last != MAX)
{
s->num[s->Last] = e;
s->Last++;
return OK;
}
return NO;
}
Status Pop(stack s, ElemType* e)
{
if (s->Last != 0)
{
*e = s->num[s->Last - 1];
s->Last--;
return OK;
}
return NO;
}
Status Stacklength(stack s)
{
return s->Last;
}
int main()
{
int b;
stack s = (stack)malloc(sizeof(Stack));
InitStack(s);
for (int i = 1; i < 15; i+=2)
{
Push(s, i);
}
for (int i = 1; i < 6; i++)
{
if (Pop(s, &b))
cout << i <<"=" << b<<endl;
}
ClearStack(s);
for (int i = 2; i < 12; i+=2)
{
Push(s, i);
}
/*for (int i = 1; i < 7; i++)
{
if(Pop(s, &b))
cout << i << "=" << b << endl;
}*/
DestoryStack(&s);
}
|