用栈模拟商店进货系统

王朝other·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

#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);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航