栈的基本运算（栈和队列）

1 2 3 4 5 0

5 4 3 2 1

``6 7 8 9 10 0``

``10 9 8 7 6 ``

``````#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define SEN 100

struct sqstack
{
char *base;
char *top;
int size;
};

struct sqstack *initEmptyStack()   /*构建空輚*/
{
struct sqstack *p;
p=(struct sqstack *)malloc(sizeof(struct sqstack));
if(p!=NULL)
{
p->base=(char *)malloc(sizeof(struct sqstack)*SEN);
if(p->base!=NULL)
{
p->top=p->base;
p->size=SEN;
return p;
}
}
else
free(p);
return NULL;
}

int push(struct sqstack *p,char a)         /*入栈*/
{
*p->top++=a;
return 0;
}

int pop(struct sqstack *p)              /*出栈*/
{
return *(--p->top);
}

int main()
{
struct sqstack *p;
int m;
p=initEmptyStack();
while(scanf("%d",&m)&&m!=0)
push(p,m);
while(p->base!=p->top)
printf("%d ",pop(p));
printf("\n");
return 0;
}``````

``````#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define SEN 100

struct sqstack
{
char *base;
char *top;
int size;
};

struct sqstack *initEmptyStack()   /*构建空輚*/
{
struct sqstack *p;
p=(struct sqstack *)malloc(sizeof(struct sqstack));
if(p!=NULL)
{
p->base=(char *)malloc(sizeof(struct sqstack)*SEN);
if(p->base!=NULL)
{
p->top=p->base;
p->size=SEN;
return p;
}
}
else
free(p);
return NULL;
}

int push(struct sqstack *p,char a)         /*入栈*/
{
*p->top++=a;
return 0;
}

int pop(struct sqstack *p)              /*出栈*/
{
return *(--p->top);
}

int main()
{
struct sqstack *p;
int m;
p=initEmptyStack();
while(scanf("%d",&m)&&m!=0)
push(p,m);
while(p->base!=p->top)
printf("%d ",pop(p));
printf("\n");
return 0;
}``````