資料結構實踐 停車場模擬 棧和佇列綜合
阿新 • • 發佈:2018-11-07
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
本文是針對資料結構基礎系列網路課程(3):棧和佇列的實踐專案。
設停車場是一個可停放n輛汽車的狹長死衚衕,南邊封口,汽車只能從北邊進出(這樣的停車場世間少有)。汽車在停車場內按車輛到達時間的先後順序,最先到達的第一輛車停放在車場的最南端,依次向北排開。若車場內已停滿n輛汽車,則後來的汽車只能在門外的候車場上等候,一旦有車開走,則排在候車場上的第一輛車即可開入。當停車場內某輛車要離開時,在它之後進入的車輛必須先退出車場為它讓路(假定停車場內設有供車輛進出的便道,所有的司機也必須在車內隨時待命),待該輛車開出大門外,其他車輛再按原次序進入車場。每輛停放在車場的車在它離開停車場時,要按停留的時間長短交納費用。試為停車場編制按上述要求進行管理的模擬程式。
提示:以棧模擬停車場,以佇列模擬車場外的候車場,有車離開時,供車輛進出的便道也應該用棧表示。按照從鍵盤讀入的輸入資料序列進行模擬管理。汽車到達和離開時,每一組輸入資料包括汽車牌照號碼(設為整數)以及到達或離去的時刻(為簡單化,也設為整數,如1,代表停車場開始營業的第1小時)。對每一組輸入資料進行操作後的輸出資訊為:若是車輛到達,則輸出汽車在停車場內或修車場上的停車位置;若是車輛離去,則輸出汽車在停車場內停留的時間和應交納的費用(在候車場上停留的時間不收費)。棧以順序結構實現,佇列以連結串列結構實現。
[參考解答]
#include <stdio.h> #include <malloc.h>#define N 10 /*停車場內最多的停車數*/#define M 10 /*候車場內最多的停車數*/ #define Price 2 /*每單位時間停車費用*/typedef struct{ int CarNo[N]; /*車牌號*/ int CarTime[N]; /*進場時間*/ int top; /*棧指標*/} SqStack; /*定義順序棧型別,用於描述停車場*/typedef struct{ int CarNo[M]; /*車牌號*/ int front,rear; /*隊首和隊尾指標*/} SqQueue; /*定義迴圈隊型別,用於描述候車場*//*以下為順序棧的基本運算演算法*/void InitStack(SqStack *&s){ s=(SqStack *)malloc(sizeof(SqStack)); s->top=-1;}int StackEmpty(SqStack *s){ return(s->top==-1);}int StackFull(SqStack *s){ return(s->top==N-1);}int Push(SqStack *&s,int e1,int e2){ if (s->top==N-1) return 0; s->top++; s->CarNo[s->top]=e1; s->CarTime[s->top]=e2; return 1;}int Pop(SqStack *&s,int &e1,int &e2){ if (s->top==-1) return 0; e1=s->CarNo[s->top]; e2=s->CarTime[s->top]; s->top--; return 1;}void DispStack(SqStack *s){ int i; for (i=s->top; i>=0; i--) printf("%d ",s->CarNo[i]); printf("\n");}/*以下為迴圈佇列的基本運算演算法*/void InitQueue(SqQueue *&q){ q=(SqQueue *)malloc (sizeof(SqQueue)); q->front=q->rear=0;}int QueueEmpty(SqQueue *q){ return(q->front==q->rear);}int QueueFull(SqQueue *q) /*判斷隊滿*/{ return ((q->rear+1)%M==q->front);}int enQueue(SqQueue *&q,int e) /*進隊*/{ if ((q->rear+1)%M==q->front) /*隊滿*/ return 0; q->rear=(q->rear+1)%M; q->CarNo[q->rear]=e; return 1;}int deQueue(SqQueue *&q,int &e) /*出隊*/{ if (q->front==q->rear) /*隊空的情況*/ return 0; q->front=(q->front+1)%M; e=q->CarNo[q->front]; return 1;}void DispQueue(SqQueue *q) /*輸出隊中元素*/{ int i; i=(q->front+1)%M; printf("%d ",q->CarNo[i]); while ((q->rear-i+M)%M>0) { i=(i+1)%M; printf("%d ",q->CarNo[i]); } printf("\n");}//main函式用於模擬停車場的工作int main(){ int comm; int no,e1,time,e2; int i,j,t; SqStack *St,*St1; //St是停車場,St1是在有車離開時,記錄為該車移開位置的車輛 SqQueue *Qu; //Qu是候車場 InitStack(St); InitStack(St1); InitQueue(Qu); do { printf("輸入指令(1:到達 2:離開 3:顯示停車場 4:顯示候車場 0:退出):"); scanf("%d",&comm); switch(comm) { case 1: /*汽車到達*/ printf("輸入車號和時間(設車號和時間均為整數): "); scanf("%d%d",&no,&time); if (!StackFull(St)) /*停車場不滿*/ { Push(St,no,time); printf(" >>停車場位置:%d\n",St->top+1); } else /*停車場滿*/ { if (!QueueFull(Qu)) /*候車場不滿*/ { enQueue(Qu,no); printf(" >>候車場位置:%d\n",Qu->rear); } else printf(" >>候車場已滿,不能停車\n"); } break; case 2: /*汽車離開*/ printf("輸入車號和時間(設車號和時間均為整數): "); scanf("%d%d",&no,&time); for (i=0; i<=St->top && St->CarNo[i]!=no; i++); //在棧中找 if (i>St->top) printf(" >>未找到該編號的汽車\n"); else { t = St->top - i; //需要出棧的車輛數目 for (j=0; j<t; j++) //for (j=i; j<=St->top; j++)1樓評論講的原錯誤寫法 { Pop(St,e1,e2); Push(St1,e1,e2); /*倒車到臨時棧St1中*/ } Pop(St,e1,e2); /*該汽車離開*/ printf(" >>%d汽車停車費用:%d\n",no,(time-e2)*Price); while (!StackEmpty(St1)) /*將臨時棧St1重新回到St中*/ { Pop(St1,e1,e2); Push(St,e1,e2); } if (!QueueEmpty(Qu)) /*隊不空時,將隊頭進棧St*/ { deQueue(Qu,e1); Push(St,e1,time); /*以當前時間開始計費*/ } } break; case 3: /*顯示停車場情況*/ if (!StackEmpty(St)) { printf(" >>停車場中的車輛:"); /*輸出停車場中的車輛*/ DispStack(St); } else printf(" >>停車場中無車輛\n"); break; case 4: /*顯示候車場情況*/ if (!QueueEmpty(Qu)) { printf(" >>候車場中的車輛:"); /*輸出候車場中的車輛*/ DispQueue(Qu); } else printf(" >>候車場中無車輛\n"); break; case 0: /*結束*/ if (!StackEmpty(St)) { printf(" >>停車場中的車輛:"); /*輸出停車場中的車輛*/ DispStack(St); } if (!QueueEmpty(Qu)) { printf(" >>候車場中的車輛:"); /*輸出候車場中的車輛*/ DispQueue(Qu); } break; default: /*其他情況*/ printf(" >>輸入的命令錯誤\n"); break; } } while(comm!=0); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204