#define stacksize 10
#include "stdio.h"
#include <malloc.h>
typedef strUCt
{int num[stacksize];
int top;
}seqstack; /*栈定义*/
typedef struct node
{unsigned num;
struct node *next;
}linkqnode;
typedef struct
{linkqnode *front,*rear;}linkqueue; /*链队列定义*/
void initstack(seqstack *s) /*初始化栈*/
{s->top=-1;}
int push(seqstack *s,int e)/*入栈*/
{if(s->top==stacksize-1){printf("Stack is full!\n");return 0;}
s->top++;
s->num[s->top]=e;return 1;
}
int pop(seqstack *s,int *e)/*出栈*/
{if(s->top==-1)return 0;
*e=s->num[s->top];
s->top--;
return 1;
}
void printstack(seqstack *s)/*输出栈中元素,栈底到栈顶*/
{int i;
for(i=0;i<=s->top;i++)
printf("%5d",s->num[i]);
printf("\n");
}
int initqueue(linkqueue *q)/*初始化队列*/
{
q->front=(linkqnode*)malloc(sizeof(linkqnode));
if(q->front!=NULL)
{q->front->next=NULL;
q->rear=q->front;return 1;
}
else return 0;
}
int enterqueue(linkqueue *q,int e)/*入队*/
{linkqnode *s;
s=(linkqnode*)malloc(sizeof(linkqnode));
if(s!=NULL)
{s->num=e;q->rear->next=s;s->next=NULL;q->rear=s;return 1;}
else return 0;
}
int delqueue(linkqueue *q,int *e)/*出队*/
{linkqnode *p;
if(q->front==q->rear)return 0;
p=q->front->next;
q->front->next=p->next;
if(q->rear==p)q->rear=q->front;
*e=p->num;
free(p);return 1;
}
main()/*主函数*/
{seqstack s;
linkqueue q;
int goodsnum;
initstack(&s);
initqueue(&q);
printf("输入进货商品号,-1结束:");
scanf("%d",&goodsnum);
while(goodsnum!=-1)
{push(&s,goodsnum);scanf("%d",&goodsnum);}
printf("\n原货架:\n");
printstack(&s);
while(s.top!=-1)
if(pop(&s,&goodsnum))enterqueue(&q,goodsnum);
while((q.front)->next!=NULL)
{delqueue(&q,&goodsnum);
push(&s,goodsnum);
}
printf("\n倒置后货架:\n");
printstack(&s);
}