102 lines
4.0 KiB
C
102 lines
4.0 KiB
C
/*
|
|
|
|
COS135 HW9a
|
|
Nicholas Pease
|
|
Dynamic FIFO Queue
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// Globals to Simplify Passing Around
|
|
// Contains pointer to queue memory
|
|
int* ptr;
|
|
// Contains current number of used slots
|
|
int current_size = 0;
|
|
// Contains total number of available slots
|
|
int total_size;
|
|
|
|
// Prints current data to screen
|
|
// Only prints slots in use, ignores empty (but not null), slots
|
|
void printStructure() {
|
|
// iterate over entire, USED queue. Newest are saves starting at the end and moving backwards.
|
|
// Start iterating after the the latest entry (total_size-current_size) and continue until end (total_size)
|
|
for(int i=total_size-current_size;i < total_size;i++) {
|
|
// Add comma if not last (or only) slot, else add newline
|
|
printf((i + 1 != total_size)?"%d, ":"%d\n",*(ptr + i));
|
|
//if (i + 1 != total_size) {printf("%d, ",*(ptr + i));}
|
|
//else {printf("%d\n",*(ptr + i));}
|
|
}
|
|
|
|
}
|
|
|
|
// Adds new addition to Queue
|
|
// int addition - New item to add
|
|
void pushToStructure(int addition) {
|
|
// If queue is not full, simply add to next available slot
|
|
if (current_size != total_size) {
|
|
// Additions are made from the backmost free slot. Starts at PTR, adds all available slots (total_size).
|
|
// Subtract number of used entries to get last full slot.
|
|
// Subtract 1 to get next open slot
|
|
*(ptr + total_size - current_size - 1) = addition;
|
|
// Add one to number of slots used, as we just filled another one
|
|
current_size++;
|
|
} else {
|
|
// If queue is full, we have an overflow. Print overflow below
|
|
printf("Overflow: %d\n",*(ptr+total_size-1));
|
|
// Increment through entire queue starting from the back to the front of the memory
|
|
for(int i = total_size - 1; i > 0; i--) {
|
|
// Since we don't care anymore about the memory we just overflowed, move all data in slots to the next slot (n to n+1)
|
|
*(ptr + i) = *(ptr + i - 1);
|
|
}
|
|
// The new addition always goes in the now free slots 0, or just *{ptr}
|
|
*(ptr) = addition;
|
|
}
|
|
}
|
|
|
|
// Main Function
|
|
// int argc - number of parameters
|
|
// char *argv[] - array of all strings passed via command line
|
|
int main(int argc, char *argv[]) {
|
|
// I have had too many segfaults to not include this. Checks to ensure number of slots is given
|
|
if (argc == 2) {
|
|
// Grab total_size from commandline and initialize queue memory with it
|
|
total_size = atoi(argv[1]);
|
|
ptr = (int*)calloc(total_size,sizeof(int));
|
|
|
|
// Input routine for user menu
|
|
char input[20] = "";
|
|
while(strncmp("quit",input,4)!= 0) {
|
|
printf("Enter command (push, print, or quit): ");
|
|
fgets(input,20,stdin);
|
|
// Strip newline character from input (fgets)
|
|
for (int i=0;i<strlen(input);i++) if (input[i] == '\n') input[i] = '\0';
|
|
// Check if the first 4 letter of string are push, then process string for remaining integer
|
|
if (strncmp("push",input,4) == 0) {
|
|
// Create and initialize null string temp[16]
|
|
// Since push is 4 letters, and fgets is capped at 20, can only be 16 characters long
|
|
char temp[16];
|
|
memset(temp,'\0',16);
|
|
// Copy all data from location 4 to the end of the input string into the temporary string
|
|
for (int i = 4; i < strlen(input);i++) {
|
|
temp[i-4] = input[i];
|
|
}
|
|
// Push parsed second arguement with atoi() converting to int
|
|
pushToStructure(atoi(temp));
|
|
}
|
|
// If the first 5 letters of the input are "print", simply call printStructure() which requires no data input
|
|
if (strncmp("print",input,5) == 0)
|
|
printStructure();
|
|
}
|
|
// Freeup memory and leave
|
|
free(ptr);
|
|
return 0;
|
|
} else {
|
|
// Forgot a commandline arguement, prompt nicely and return.
|
|
printf("Please enter number of slots in queue\n");
|
|
return 1;
|
|
}
|
|
|
|
} |