问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面呢的车要先退出,待它走后再依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1)建立三个数据结构分别是:停放队列、让路栈、等候队列。
(2)输入数据模拟管理过程,数据(入或出,车号)。
停车管理系统是C语言中队列和栈比较简单的应用,需要注意的是停车队列、等候队列、让路栈结构体的构建。在写代码时,出队列入栈和出栈入队列时,指针容易出错而造成段错误,应当注意。我所写的代码如下:
//定义结构体
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
|
#include <stdio.h>
#include <stdlib.h>
#define F 0
#define T 1
#define MAX 3
typedef struct Node //数据
{
int number;
int time ;
}Node;
typedef struct QueueNode //队列结点
{
struct Node infom;
struct QueueNode * next;
}*QueueNode;
typedef struct LinkQueue //链队列
{
struct QueueNode * front;
struct QueueNode * rear;
}LinkQueue;
typedef struct stack //栈结点
{
struct Node data;
struct stack *next;
}*StackNode;
typedef struct LinkStack //链栈
{
StackNode top;
int count;
}LinkStack;
|
//函数实现
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway, int num, int t); //菜单
int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway); //初始化
int linklength(LinkQueue q); //查看长度
int enqueue(LinkQueue *q, int num, int t); //入队列
int dequeue(LinkQueue *q, int *num, int *t); //出队列
void park1(LinkQueue *wait,LinkQueue *park); //停车函数
int push(LinkStack *s, int num, int t); //入栈
int pop(LinkStack *s, int *num, int *t); //出栈
void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway, int num, int t); //离开函数
void view3(LinkQueue wait,LinkQueue park); //查看停车场状态
int main()
{
LinkQueue wait;
LinkQueue park;
LinkStack giveway;
int num = 0;
int t = 0;
init(&wait,&park,&giveway);
menu(&wait,&park,&giveway,num,t);
return 0;
}
int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway)
{
QueueNode newnode1 = (QueueNode) malloc ( sizeof ( struct QueueNode));
if (NULL == newnode1)
{
return F;
}
newnode1->next = NULL;
wait->front = newnode1;
wait->rear = newnode1;
QueueNode newnode2 = (QueueNode) malloc ( sizeof ( struct QueueNode));
if (NULL == newnode2)
{
return F;
}
newnode2->next = NULL;
park->front = newnode2;
park->rear = newnode2;
giveway->top = NULL;
giveway->count = 0;
}
void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway, int num, int t)
{
printf ( "**********Welcome to our Car Parking !**********\n" );
printf ( "********** Please choose function **********\n" );
printf ( "********** 1 : park. **********\n" );
printf ( "********** 2 : leave. **********\n" );
printf ( "********** 3 : view. **********\n" );
printf ( "********** 4 : exit. **********\n" );
int option;
scanf ( "%d" ,&option);
switch (option)
{
case 1:{
park1(wait,park);
printf ( "停车完成!\n" );
menu(wait,park,giveway,num,t);
break ;
}
case 2:{
leave2(wait,park,giveway,num,t);
menu(wait,park,giveway,num,t);
break ;
}
case 3:{
view3(*wait,*park);
menu(wait,park,giveway,num,t);
break ;
}
case 4:{
printf ( "********** 欢迎再次使用,谢谢! **********\n" );
break ;
}
default :{
printf ( "********** 请输入正确的指令! **********\n" );
menu(wait,park,giveway,num,t);
break ;
}
}
}
int linklength(LinkQueue q)
{
int i = 0;
while (q.front != q.rear)
{
i++;
q.front = q.front->next;
}
return i;
}
int enqueue(LinkQueue *q, int num, int t)
{
QueueNode newnode = (QueueNode) malloc ( sizeof ( struct QueueNode));
if (NULL == newnode)
{
return F;
}
newnode->infom.number = num;
newnode->infom. time = t;
newnode->next = NULL;
q->rear->next = newnode;
q->rear = newnode;
return T;
}
int dequeue(LinkQueue *q, int *num, int *t)
{
if (q->front == q->rear)
{
printf ( "the queue is empty!\n" );
return F;
}
*num = q->front->next->infom.number;
*t = q->front->next->infom. time ;
QueueNode temp = q->front->next;
q->front->next = temp->next;
if (temp->next == NULL)
{
q->rear = q->front;
}
free (temp);
return T;
}
void park1(LinkQueue *wait,LinkQueue *park)
{
printf ( "请输入车号和停车时间\n" );
int num,t;
scanf ( "%d,%d" ,&num,&t);
if (linklength(*park) >= MAX)
{
printf ( "停车场已满,进入等待区!\n" );
enqueue(wait,num,t);
}
else
{
enqueue(park,num,t);
}
}
int push(LinkStack *s, int num, int t)
{
StackNode newnode = (StackNode) malloc ( sizeof ( struct stack));
if (NULL == newnode)
{
return F;
}
newnode->data.number = num;
newnode->data. time = t;
newnode->next = s->top;
s->top = newnode;
s->count++;
return T;
}
int pop(LinkStack *s, int *num, int *t)
{
if (0 == s->count)
{
printf ( "the stack is empty !\n" );
return F;
}
*num = s->top->data.number;
*t = s->top->data. time ;
StackNode temp = s->top;
s->top = s->top->next;
free (temp);
s->count--;
return T;
}
void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway, int num, int t)
{
printf ( "请输入要离开车的车号\n" );
int leavenumber;
scanf ( "%d" ,&leavenumber);
int i = 0;
QueueNode head = park->front;
while (head != park->rear)
{
if (head->next->infom.number != leavenumber)
{
head = head->next;
i++;
}
else
break ;
}
int j = 0;
if (i <= MAX-1)
{
while (j != i)
{
dequeue(park,&num,&t);
push(giveway,num,t);
j++;
}
dequeue(park,&num,&t);
}
else
{
printf ( "查无此车!\n" );
}
while (giveway->top != NULL)
{
pop(giveway,&num,&t);
enqueue(park,num,t);
}
if (linklength(*wait) != 0)
{
dequeue(wait,&num,&t);
enqueue(park,num,t);
}
}
void view3(LinkQueue wait,LinkQueue park)
{
printf ( "******************** 目前停车场状况 ********************\n" );
printf ( "停车场共%d个车位,当前停车场共有%d量车,等待区共有%d量车\n" ,
MAX,linklength(park),linklength(wait));
printf ( "**************************************************************\n" );
printf ( "车 号:" );
QueueNode head1 = park.front;
QueueNode head2 = park.front;
while (head1 != park.rear)
{
printf ( "%d " ,head1->next->infom.number);
head1 = head1->next;
}
printf ( "\n" );
printf ( "停车时间:" );
while (head2 != park.rear)
{
printf ( "%d " ,head2->next->infom. time );
head2 = head2->next;
}
printf ( "\n" );
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_33557384/article/details/77600922