首页 > 代码库 > 孤独的运货员

孤独的运货员

先附上题目要求:

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

在解题的时候用到了栈和队列,其中货车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 }