|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +#define MAXSIZE 5 |
| 5 | + |
| 6 | +int addToQueue(int myQueue[MAXSIZE], int* start, int* finish); |
| 7 | +int removeFromQueue(int theQueue[MAXSIZE], int* begin, int* end); |
| 8 | + |
| 9 | +int main() |
| 10 | +{ |
| 11 | + int queue[MAXSIZE] = {0}; |
| 12 | + char action = '\0'; |
| 13 | + int rear = -1; |
| 14 | + int front = 0; |
| 15 | + do |
| 16 | + { |
| 17 | + printf("What do you want to do? \ |
| 18 | + \n\r[i for insert, r for remove, q for quit] "); |
| 19 | + scanf(" %c",&action); |
| 20 | + if (action == 'i') |
| 21 | + rear = addToQueue(queue, &front, &rear); |
| 22 | + else if (action == 'r') |
| 23 | + front = removeFromQueue(queue, &front, &rear); |
| 24 | + else if (action == 'q') |
| 25 | + printf("~$\n"); |
| 26 | + else |
| 27 | + printf("That's not a valid input.\n"); |
| 28 | + } |
| 29 | + while (action != 'q'); |
| 30 | + return 0; |
| 31 | +} |
| 32 | + |
| 33 | +int addToQueue(int myQueue[MAXSIZE], int* start, int* finish) |
| 34 | +{ |
| 35 | + int addInt = 0; |
| 36 | + if ((*start == 0) && (*finish == MAXSIZE - 1)) |
| 37 | + printf("Can't insert, the array is full \n"); |
| 38 | + else |
| 39 | + { |
| 40 | + print("Enter an integer to be inserted into the queue: "); |
| 41 | + scanf("%d", &addInt); |
| 42 | + *finish = ((*finish) + 1) % MAXSIZE; |
| 43 | + myQueue[*finish] = addInt; |
| 44 | + } |
| 45 | + return *finish; |
| 46 | +} |
| 47 | + |
| 48 | +int removeFromQueue(int theQueue[MAXSIZE], int* begin, int* end) |
| 49 | +{ |
| 50 | + if ((*begin == 0) && (*end == -1)) |
| 51 | + printf("Cannot remove, the queue is empty\n"); |
| 52 | + else if (theQueue[*begin] == theQueue[*end]) |
| 53 | + { |
| 54 | + printf("%d removed from the queue\n",theQueue[*begin]); |
| 55 | + theQueue[*begin] = 0; |
| 56 | + *begin = 0; |
| 57 | + *end = -1; |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + printf("%d removed from queue\n",theQueue[*begin]); |
| 62 | + theQueue[*begin] = 0; |
| 63 | + *begin = ((*begin) + 1) % MAXSIZE; |
| 64 | + } |
| 65 | + return *begin; |
| 66 | +} |
0 commit comments