孤独的运货员

先附上题目要求:

下面附上数据结构框图,本人整理(勿喷!)

在解题的时候用到了栈和队列,其中货车FILO用到了栈的结构,各国运货平台FIFO用到了队列结构,动态分配各运货平台,其实题目读懂之后模拟整个运货平台分理过程也没有那么难,关键是做好数据结构!

下面附上代码:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 #define MAX_CAR_CAPACITY 100
  5 typedef struct Node {
  6     int goodsDestination;   //货物目的地
  7     struct Node *Link;      //结点元素指针
  8 } QNode;
  9 typedef struct {
 10     int goodsQuantity;      //货物总量
 11     QNode *front;       //队列头指针
 12     QNode *rear;        //队列尾指针,指向队尾下一元素即插入元素位置
 13 } LinkQueue;
 14 typedef struct {         //货车栈,FILO
 15     int *top;
 16     int *base;
 17 } Stack;
 18 
 19 void InitQueue(LinkQueue *Q);
 20 void DeQueue(LinkQueue *Q,int *goodsDes);
 21 void EnQueue(LinkQueue *Q,int goodsDestination);
 22 void DestroyQueue(LinkQueue *Q);
 23 void PrintQueue(LinkQueue Q);
 24 
 25 int main()
 26 {
 27     int testCnt,stations,carCapacity,storeCapacity,iCnt,jCnt,leftNum,goodsDes,kCircle,usedTime,flag;
 28     LinkQueue *goods,*sptr;               //货运站队列,动态分配
 29     Stack car;                            //货车栈
 30 
 31     car.base = (int *)malloc(MAX_CAR_CAPACITY * sizeof(int));
 32     if(!car.base)   exit(1);
 33 
 34     scanf("%d",&testCnt);
 35     while(testCnt--) {
 36         scanf("%d%d%d",&stations,&carCapacity,&storeCapacity);
 37         goods = (LinkQueue *)malloc(stations * sizeof(LinkQueue));//动态分配环中站
 38         //初始化环中站
 39         for(iCnt = 0; iCnt < stations; iCnt++) {
 40             scanf("%d",&leftNum);
 41             //goods++ 地址跳过12个字节,一次跳过一个队列结构体内存地址数
 42             //printf("%p\n",goods + iCnt);//&goods[iCnt];goods + iCnt * sizeof(LinkQueue);
 43             //printf("%p\n",&goods[iCnt]);
 44             sptr = goods + iCnt;
 45             InitQueue(sptr);
 46             for(jCnt = 0; jCnt < leftNum; jCnt++) {
 47                 scanf("%d",&goodsDes);
 48                 EnQueue(sptr,goodsDes);
 49             }
 50         }
 51 #if 0      //输出测试
 52         printf("OK\r\n");
 53         for(iCnt = 0; iCnt < stations; iCnt++) {
 54             sptr = goods + iCnt;
 55             PrintQueue(*sptr);      // 取地址内容
 56             printf("\n");
 57         }
 58 #endif
 59 
 60         //分理中心货物模拟
 61         usedTime = 0;
 62         car.top = car.base;
 63         for(kCircle = 0;; kCircle = ((++kCircle)%stations)) {//= (++kCircle)%stations)
 64             flag = 1;       //模拟处理标志位
 65             sptr = goods + kCircle;
 66             //卸货
 67             while(car.top != car.base) {
 68                 if(*(--car.top) == kCircle + 1) { //卸到A平台
 69                     usedTime++;
 70                 } else if(sptr->goodsQuantity < storeCapacity) { //卸到B平台
 71                     EnQueue(sptr,*car.top);
 72                     usedTime++;
 73                 } else {
 74                     car.top++;
 75                     break;
 76                 }
 77             }
 78             //装货
 79             while((car.top - car.base) < carCapacity && (sptr->goodsQuantity != 0)) {
 80                 DeQueue(sptr,&goodsDes);
 81                 *car.top = goodsDes;
 82                 car.top++;
 83                 usedTime++;
 84             }
 85             for(iCnt = 0; iCnt < stations; iCnt++) {
 86                 sptr = goods + iCnt;
 87                 if(sptr->goodsQuantity != 0) {
 88                     flag = 0;
 89                     break;
 90                 }
 91             }
 92             if(flag && (car.top == car.base))       //处理完毕,跳出循环
 93                 break;
 94             usedTime += 2;
 95         }
 96         printf("%d\n",usedTime);
 97     }
 98     return 0;
 99 }
100 void InitQueue(LinkQueue *Q)
101 {
102     Q->rear = Q->front = (QNode *)malloc(sizeof(QNode));//头结点,不存放数据
103     if(!Q->front)   exit(1);
104 
105     Q->front->Link = NULL;
106     Q->goodsQuantity = 0;
107 }
108 void DeQueue(LinkQueue *Q,int *goodsDes)//队头删除
109 {
110     QNode *QueuePtr = Q->front->Link;
111     if(Q->front == Q->rear) exit(1);        //空队列
112 
113     *goodsDes =QueuePtr->goodsDestination;
114     Q->goodsQuantity--;
115     Q->front->Link = QueuePtr->Link;
116     if(QueuePtr == Q->rear)
117         Q->rear = Q->front;
118     free(QueuePtr);
119 }
120 void EnQueue(LinkQueue *Q,int goodsDestination)//队尾插入
121 {
122     QNode *QueuePtr = (QNode *)malloc(sizeof(QNode));
123     if(!QueuePtr)   exit(1);
124 
125     QueuePtr->goodsDestination = goodsDestination;
126     QueuePtr->Link = NULL;
127     Q->rear->Link = QueuePtr;
128     Q->rear = QueuePtr;
129     Q->goodsQuantity++;
130 }
131 void DestroyQueue(LinkQueue *Q)
132 {
133     while(Q->front) {
134         Q->rear = Q->front; //摘下
135         Q->front = Q->front->Link;//移至下一结点
136         free(Q->rear);//释放
137     }
138 }
139 void PrintQueue(LinkQueue Q)
140 {
141     if(!Q.front) exit(1);
142     printf("%d\t",Q.goodsQuantity);
143     while(Q.front->Link) {
144         printf("%d\t",Q.front->Link->goodsDestination);
145         Q.front->Link = Q.front->Link->Link;
146     }
147 }

 

转载于:https://www.cnblogs.com/wujichaomx/p/3738216.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值