-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaperCircularArray.c
More file actions
308 lines (250 loc) · 8.42 KB
/
paperCircularArray.c
File metadata and controls
308 lines (250 loc) · 8.42 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Program: paperCircularArray.c
* Purpose: This is intended to show a basic circular array (buffer) and how it functions.
* This was originally coded as part of an extra credit assignment and it really helped me learn more about
* data structures and how memory is handled in C programs.
*
* Edits from the original code: The original code did not make use of .h files or function prototypes as this was not
* a requirement for that class at the time and I was still new to C coding. I have since gone back and created the necessary
* header file (paperCircularArray.h) to encapsulate the struct for the buffer and the macro for the ARRAY_MAX_SIZE and the necessary function prototypes.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "paperCircularArray.h"
// Initialize the CircularArray
CircularArray* initializeCircularArray()
{
// Allocate memory for the CircularArray
CircularArray* newArray = (CircularArray*)malloc(sizeof(CircularArray));
// If the newArray is NULL, that means there was an error allocating memory
if (newArray == NULL)
{
printf("Error allocating memory\n");
exit(EXIT_FAILURE);
}
// Set the structure members to default values (0)
newArray->elementCount = 0;
newArray->read = 0;
newArray->write = 0;
return newArray;
}
// Checks if the passed in CircularArray is EMPTY
bool isEmpty(CircularArray* arrayToCheck)
{
// If the elementCount is 0, the CircularArray is empty
if (arrayToCheck->elementCount == 0)
{
printf("Array is empty!\n");
return true;
}
// Otherwise the array is NOT empty
return false;
}
// Checks if the passed in CircularArray is FULL
bool isFull(CircularArray* arrayToCheck)
{
// If the elementCount is equal to the ARRAY_MAX_SIZE macro, it is full
if (arrayToCheck->elementCount == ARRAY_MAX_SIZE)
{
printf("Array is full!\n");
return true;
}
// Otherwise it is NOT full
return false;
}
// Insert a value into the CircularArray
void insertValueIntoArray(CircularArray* arrayToAddTo, int newValue)
{
// Check if the CircularArray is full (cannot insert a new value)
if (isFull(arrayToAddTo))
{
return;
}
// Otherwise insert the new value:
// Increase the element count
arrayToAddTo->elementCount++;
// Insert the newValue into the array using the arrays current "write" location
arrayToAddTo->array[arrayToAddTo->write] = newValue;
// Update the write location after the data has been added, ensure it is within the bounds of the array size
arrayToAddTo->write = (arrayToAddTo->write + 1) % ARRAY_MAX_SIZE;
}
// Dequeue(remove) a value from the CircularArray
int dequeueFromArray(CircularArray* arrayToDequeue)
{
// Check if the CircularArray is empty (isEmpty will show an error if needed)
if (isEmpty(arrayToDequeue))
{
return -1;
}
// Otherwise, begin dequeue process
// Get the value that will be dequeued based on the current CircularArray READ location
int valueToDequeue = arrayToDequeue->array[arrayToDequeue->read];
// Decrement the elementCount (since we are removing an value)
arrayToDequeue->elementCount--;
// Update the read location and ensure it is within the bounds of the array size
arrayToDequeue->read = (arrayToDequeue->read + 1) % ARRAY_MAX_SIZE;
return valueToDequeue;
}
// Print the CircularArray
void printCircularArray(CircularArray* arrayToPrint)
{
// Check if the CircularArray is empty
if (isEmpty(arrayToPrint))
{
return;
}
// Otherwise make a copy of the current index to use later
int currentIndex = arrayToPrint->read;
int count = 0;
// Iterate over the values, as long as count is less than the number of elements
while (count < arrayToPrint->elementCount)
{
// Print the index and the value stored there
printf("Circular Index: %d, Value at index: %d\n", currentIndex, arrayToPrint->array[currentIndex]);
count++; // Increment the count
// Set the currentIndex to the next index location to print the next value, keep within bounds of array size
currentIndex = (currentIndex + 1) % ARRAY_MAX_SIZE;
}
}
// Search for a value in the CircularArray
void searchCircularArray(CircularArray* arrayToSearch, int valueToFind)
{
// Check if the CircularArray is empty
if (isEmpty(arrayToSearch))
{
return;
}
// Begin searching from the current read location
int currentIndex = arrayToSearch->read;
int count = 0;
// Iterate over the values, as long as count is less than the number of elements
while (count < arrayToSearch->elementCount)
{
// Check if the value at the current index is the same as the one passed in
if (arrayToSearch->array[currentIndex] == valueToFind)
{
// Item was found
printf("Found item at: %d\nValue being searched for: %d\nValue in array: %d", currentIndex, valueToFind, arrayToSearch->array[currentIndex]);
return;
}
// Keep iterating by incrementing the count and the current index
count++;
currentIndex = (currentIndex + 1) % ARRAY_MAX_SIZE;
}
// If the value was not found
printf("Value not found!\n");
}
// The main entry point for testing the CircularArray
int main(void)
{
// Initialize the circular array
CircularArray* theArray = initializeCircularArray();
// Check if the array is empty
isEmpty(theArray);
// Check if the array is full
isFull(theArray);
printf("\n");
// Inserting values
insertValueIntoArray(theArray, 100);
printf("Item 1 insertion:\n");
printCircularArray(theArray);
insertValueIntoArray(theArray, 200);
printf("Item 2 insertion:\n");
printCircularArray(theArray);
insertValueIntoArray(theArray, 300);
printf("Item 3 insertion:\n");
printCircularArray(theArray);
insertValueIntoArray(theArray, 400);
printf("Item 4 insertion:\n");
printCircularArray(theArray);
insertValueIntoArray(theArray, 500);
printf("Item 5 insertion:\n");
printCircularArray(theArray);
// Dequeue values
printf("Dequeue 1: \n");
printf("%d\n", dequeueFromArray(theArray));
printCircularArray(theArray);
printf("Dequeue 1: \n");
printf("%d\n", dequeueFromArray(theArray));
printCircularArray(theArray);
printf("Dequeue 1: \n");
printf("%d\n", dequeueFromArray(theArray));
printCircularArray(theArray);
// Insert more values
insertValueIntoArray(theArray, 100);
printf("Item 1 insertion:\n");
printCircularArray(theArray);
insertValueIntoArray(theArray, 200);
printf("Item 2 insertion:\n");
printCircularArray(theArray);
printf("Access item at index 4: %d\n", theArray->array[4]);
insertValueIntoArray(theArray, 100);
printf("Item 1 insertion:\n");
printCircularArray(theArray);
insertValueIntoArray(theArray, 200);
printf("Item 2 insertion:\n");
printCircularArray(theArray);
/*
*
* The lines below can be uncommented for more testing
*
*/
//printf("After filling array: \n");
//printCircularArray(theArray);
//printf("\nInsertion attempted on full array: \n");
//insertValueIntoArray(theArray, 600);
//printCircularArray(theArray);
//printf("\nDequeue 100: \n");
//printf("Value dequeued: %d\n", dequeueFromArray(theArray));
//printCircularArray(theArray);
//insertValueIntoArray(theArray, 5010);
//printf("\nAdded a new element (5010): \n");
//printCircularArray(theArray);
//
//printf("\nDequeue 1: \n");
//printf("%d\n", dequeueFromArray(theArray));
//printf("Out 1: \n");
//printCircularArray(theArray);
//printf("Dequeue 1: \n");
//printf("%d\n", dequeueFromArray(theArray));
//printf("Out 2: \n");
//printCircularArray(theArray);
//printf("Dequeue 1: \n");
//printf("%d\n", dequeueFromArray(theArray));
//printf("Out 3: \n");
//printCircularArray(theArray);
//printf("Dequeue 1: \n");
//printf("%d\n", dequeueFromArray(theArray));
//printf("Out 4: \n");
//printCircularArray(theArray);
//printf("Dequeue 1: \n");
//printf("%d\n", dequeueFromArray(theArray));
//printf("Out 5: \n");
//printCircularArray(theArray);
//printf("Dequeue 1: \n");
//printf("%d\n", dequeueFromArray(theArray));
//printf("Out 6: \n");
//printCircularArray(theArray);
//insertValueIntoArray(theArray, 100);
//printf("Out 7: \n");
//printCircularArray(theArray);
//insertValueIntoArray(theArray, 200);
//printf("Out 8: \n");
//printCircularArray(theArray);
//insertValueIntoArray(theArray, 300);
//printf("Out 9: \n");
//printCircularArray(theArray);
//insertValueIntoArray(theArray, 400);
//printf("Out 10: \n");
//printCircularArray(theArray);
//insertValueIntoArray(theArray, 500);
//printf("Out 11: \n");
//printCircularArray(theArray);
//insertValueIntoArray(theArray, 600);
//printf("Out 12: \n");
//printCircularArray(theArray);
//searchCircularArray(theArray, 400);
free(theArray); // Free allocated memory
return 0;
}