forked from robertocarlosmedina/bus-controler-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpessoas copy.c
199 lines (153 loc) · 4.39 KB
/
pessoas copy.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
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#define CAPACIDADE 31 // máxima capacidade do autocarro
/* defenição estrutura pessoa */
typedef struct pessoa
{
int id;
struct pessoa * next;
} Fila; // Fila é uma typedef da struct pessoa
/* para controlar quantas pessoas estão no autocarro */
unsigned int nrPessoasAutocarro = 0;
int enqueue(Fila ** rear, Fila ** front, int id);
int dequeue(Fila ** front);
int isEmpty();
int isFull();
int main()
{
int ch, id,nrPessoasEntrada, nrPessoasSaida, i;
Fila *rear, *front;
rear = NULL;
front = NULL;
srand(time(NULL));
/* Run indefinitely until user manually terminates */
while (1)
{
/* Queue menu */
printf("--------------------------------------------\n");
printf(" QUEUE LINKED LIST IMPLEMENTATION PROGRAM \n");
printf("--------------------------------------------\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Size\n");
printf("4. Get Rear\n");
printf("5. Get Front\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");
printf("Select an option: ");
scanf("%d", &ch);
/* Menu control switch */
switch (ch)
{
case 1:
nrPessoasEntrada = 0 + rand()%(CAPACIDADE-nrPessoasAutocarro);
// Enqueue function returns 1 on success
// otherwise 0
if (!isFull()){
for(i=0; i<nrPessoasEntrada; i++){
if (enqueue(&rear, &front, nrPessoasAutocarro+1))
printf("Element added to queue.");
else
printf("Queue is full.");
}
}
break;
case 2:
if(!isEmpty()){
nrPessoasSaida = 0 + rand()%(nrPessoasAutocarro);
// on success dequeue returns element removed
// otherwise returns INT_MIN
for(i=0; i<nrPessoasSaida; i++){
id = dequeue(&front);
if (id == INT_MIN)
printf("Queue is empty.");
else
printf("Data => %d", id);
}
}
break;
case 0:
printf("Exiting from app.\n");
exit(0);
default:
printf("Invalid choice, please input number between (0-5).");
break;
}
printf("\n\n");
}
}
/**
* Enqueues/Insert an element at the rear of a queue.
* Function returns 1 on success otherwise returns 0.
*/
int enqueue(Fila ** rear, Fila ** front, int id)
{
Fila * newNode = NULL;
// Check queue out of capacity error
if (isFull())
{
return 0;
}
// Create a new node of queue type
newNode = (Fila *) malloc (sizeof(Fila));
// Assign data to new node
newNode->id = id;
// Initially new node does not point anything
newNode->next = NULL;
// Link new node with existing last node
if ( (*rear) )
{
(*rear)->next = newNode;
}
// Make sure newly created node is at rear
*rear = newNode;
// Link first node to front if its NULL
if ( !( *front) )
{
*front = *rear;
}
// Increment quque size
nrPessoasAutocarro++;
return 1;
}
/**
* Dequeues/Removes an element from front of the queue.
* It returns the element on success otherwise returns
* INT_MIN as error code.
*/
int dequeue(Fila ** front)
{
Fila *toDequque = NULL;
int id = INT_MIN;
// Queue empty error
if (isEmpty())
{
return INT_MIN;
}
// Get element and data to dequeue
toDequque = *front;
id = toDequque->id;
// Move front ahead
*front = (*front)->next;
// Decrement size
nrPessoasAutocarro--;
// Clear dequeued element from memory
free(toDequque);
return id;
}
/**
* Checks, if queue is empty or not.
*/
int isEmpty()
{
return (nrPessoasAutocarro <= 0);
}
/**
* Checks, if queue is within the maximum queue capacity.
*/
int isFull()
{
return (nrPessoasAutocarro >= CAPACIDADE);
}