char elemtype;
/*商品货架管理:商品货架可以看成一个栈,栈顶商品的生产日期最近,上货时,需要倒货
架,以保证生产日期较近的商品在较下的位置,用队列和栈最为周转,实现上述管理过程*/
#include "stdio.h"
#include "stdlib.h"
typedef
typedef struct node
{
elemtype date;
struct node *next;
}Node,*LinkStack;
void InitStack(LinkStack *S)
{
*S=(LinkStack)malloc(sizeof(Node));
(*S)->next=NULL;
}
int Push(LinkStack top,elemtype x)
{Node *temp;
temp=( Node*)malloc(sizeof(Node));
if(temp==NULL) return 0;
else
{temp->date =x;
temp->next=top->next;
top->next=temp;
return 1;
}}
int Pop(LinkStack top,elemtype *x)
{
Node *temp;
temp=top->next;
if(temp==NULL) return 0;
else {
top->next=temp->next;
*x=temp->date ;
free(temp);
return 1;
}
}
void main()
{
char c;
Node *p,*q;
LinkStack top;
InitStack(&top);
printf("请输出原始货架上的商品,每一个商品用一个字母表示,以$结束\n");
c=getchar();
while(c!='$')
{
Push(top,c);
c=getchar();
}
printf("原始货架上的商品从栈顶到栈底依次为:\n");
p=top->next;
while(p!=NULL)
{
printf("%c\n",p->date );
p=p->next ;
}
printf("请输出放入的新商品,以$结束");
c=getchar();
while(c!='$')
{
Push(top,c);
c=getchar();
}
printf("新的货架上的商品从栈顶到栈底依次为:\n");
q=top->next ;
while(q!=NULL)
{
printf("%c\n",q->date);
q=q->next ;
}
}