龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

用栈模拟商店进货系统

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
#define stacksize 10 #include "stdio.h" #include typedef strUCt {int num[stacksize]; int top; }seqstack; /*栈定义*/ typedef struct node {unsigned num; struct node *next; }linkqnode; typedef struct {linkqnode *front,*rear;}linkqueue; /*链队

#define stacksize 10

  #include "stdio.h"

  #include

  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!

");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("

");

  }

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("

原货架:

");

   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("

倒置后货架:

");

   printstack(&s);

   }

  

精彩图集

赞助商链接