|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
下面是代码:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define Maxsize 50
#define Increase 10
typedef char SElemType;
typedef struct
{
SElemType * base, * top;
int stacksize;
}SqStack;
void InitStack(SqStack *s)
{
s->base = (SElemType *)malloc(Maxsize * sizeof(SElemType));
if(!s->base)
{
printf("内存分配失败!程序将终止!");
exit(-1);
}
s->base = s->top;
s->stacksize = Maxsize;
return ;
}
void PushStack(SqStack *s, SElemType e)
{
if(!s->base)
exit(-1);
*s->top = e;
s->top++;
return ;
}
void PopStack(SqStack *s, SElemType *e)
{
if(s->base != s->top)
{
*e = *(--s->top);
}
return ;
}
bool EmptyStack(SqStack *s)
{
if(s->base == s->top)
return true;
else
return false;
}
void Traverse(SqStack *s)
{
SqStack *q = s;
if(!q)
{
printf("内存分配失败,程序终止!");
exit(-1);
}
while(q->base != q->top)
{
printf("%d", q->top);
q->top--;
}
}
int main()
{
SqStack Operator;
int c;
InitStack(&Operator);
PushStack(&Operator, 1);
PushStack(&Operator, 3);
PushStack(&Operator, 5);
PushStack(&Operator, 7);
Traverse(&Operator);
PopStack(&Operator,&c);
Traverse(&Operator);
return 0;
} |
|