-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuefunctions.c
More file actions
143 lines (133 loc) · 3.66 KB
/
Copy pathqueuefunctions.c
File metadata and controls
143 lines (133 loc) · 3.66 KB
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
#include "queuefunctions.h"
//this is fixed, and puts things on the back
//not the front
int enqueueFloor(struct ElevatorData *ed, int floor){
int success = QUEUE_SUCCESS;
//if the queue is full, expand it
if(!(ed->floorQueue[ed->queueSize - 1] == QUEUE_EMPTY_FLAG)){
success = increaseQueueSize(ed);
}
if(success == QUEUE_SUCCESS){ //if expansion was successful
//add the value to the back
int firstEmpty = 0;
int i;
for(i = 0; i < ed->queueSize; i++){
if(ed->floorQueue[i] == QUEUE_EMPTY_FLAG){
firstEmpty = i;
i = ed->queueSize;
break;
}
}
ed->floorQueue[firstEmpty] = floor;
return QUEUE_SUCCESS;
}
else{ //if expansion failed
return QUEUE_ERROR;
}
}
//returns the number of values in the queue
int getQueueSize(struct ElevatorData *ed){
//runs through the queue until it hits a -1 value
//then returns the count
int i;
int size = -1;
for(i = 0; i < ed->queueSize; i++){
if(ed->floorQueue[i] == QUEUE_EMPTY_FLAG){
size = i - 1;
}
}
if(size == -1){
return i;
}
return size;
}
int enqueueFloorToFront(struct ElevatorData *ed, int floor){
//move all data in ed back a section
//empty spaces of ed will be cointed as a -1
//if no -1 spaces exist, return error code
int i = 0;
int hasEmpty = 0;
for(i = 0; i < ed->queueSize; i++){
if(ed->floorQueue[i] == QUEUE_EMPTY_FLAG){
hasEmpty = 1;
}
}
//if there is no empty position
//increase the size of the queue
if(hasEmpty == 0){
int hasEmpty = increaseQueueSize(ed);
}
else{
hasEmpty = QUEUE_SUCCESS;
}
//if it was successfully expanded
if(hasEmpty == QUEUE_SUCCESS){
//move all values forward by 1
int i;
for(i = ed->queueSize - 1; i > 0; i--){
ed->floorQueue[i] = ed->floorQueue[i - 1];
}
//set the 0th space to be the given value
ed->floorQueue[0] = floor;
return QUEUE_SUCCESS;
}
else{ //if expansion of queue failed
return QUEUE_ERROR;
}
}
int dequeueFloor(struct ElevatorData *ed){
//if the 0th value is empty
//return error
//otherwise get the value and move values towards 0, set backmost value to empty
if(ed->floorQueue[0] == QUEUE_EMPTY_FLAG){
return QUEUE_ERROR;
}
else{
//move all values forward by one
int returnval = ed->floorQueue[0];
int i;
for(i = 0; i < ed->queueSize - 1; i++){
ed->floorQueue[i] = ed->floorQueue[i + 1];
}
//set the last value to -1
ed->floorQueue[ed->queueSize - 1] = QUEUE_EMPTY_FLAG;
//print the full QUEUE
return returnval;
}
}
int increaseQueueSize(struct ElevatorData *ed){
//create and initialize the storage array
int oldSize = ed->queueSize;
int storeArray[oldSize];
int i;
for(i = 0; i < oldSize; i++){
storeArray[i] = ed->floorQueue[i];
}
//delete the old array
free(ed->floorQueue);
//create the new array
//make it have twice the size of the old one
ed->queueSize = (ed->queueSize * 2);
ed->floorQueue = malloc(sizeof(int[ed->queueSize]));
//if the new array wasn't initialized return the error value
if(ed->floorQueue == NULL){
return QUEUE_ERROR;
}
//initialize the new array to have the same values as the old one
for(i = 0; i < oldSize; i++){
ed->floorQueue[i] = storeArray[i];
}
//set the rest of the values to the empty flag
for(;i < ed->queueSize; i++){
ed->floorQueue[i] = QUEUE_EMPTY_FLAG;
}
return QUEUE_SUCCESS;
}
void printFullQueue(struct ElevatorData *ed){
int i = 0;
printf("%s\n", "---Beginning Queue Print---");
for(i = 0; i < getQueueSize(ed); i++){
printf("%s%d%s%d\n", "At Position: ", i, " Is Value: ", ed->floorQueue[i]);
}
printf("%s\n", "---Ending Queue Print---");
}