-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw2_array_queue.c
More file actions
66 lines (61 loc) · 1.7 KB
/
hw2_array_queue.c
File metadata and controls
66 lines (61 loc) · 1.7 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
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
int addToQueue(int myQueue[MAXSIZE], int* start, int* finish);
int removeFromQueue(int theQueue[MAXSIZE], int* begin, int* end);
int main()
{
int queue[MAXSIZE] = {0};
char action = '\0';
int rear = -1;
int front = 0;
do
{
printf("What do you want to do? \
\n\r[i for insert, r for remove, q for quit] ");
scanf(" %c",&action);
if (action == 'i')
rear = addToQueue(queue, &front, &rear);
else if (action == 'r')
front = removeFromQueue(queue, &front, &rear);
else if (action == 'q')
printf("~$\n");
else
printf("That's not a valid input.\n");
}
while (action != 'q');
return 0;
}
int addToQueue(int myQueue[MAXSIZE], int* start, int* finish)
{
int addInt = 0;
if ((*start == 0) && (*finish == MAXSIZE - 1))
printf("Can't insert, the array is full \n");
else
{
print("Enter an integer to be inserted into the queue: ");
scanf("%d", &addInt);
*finish = ((*finish) + 1) % MAXSIZE;
myQueue[*finish] = addInt;
}
return *finish;
}
int removeFromQueue(int theQueue[MAXSIZE], int* begin, int* end)
{
if ((*begin == 0) && (*end == -1))
printf("Cannot remove, the queue is empty\n");
else if (theQueue[*begin] == theQueue[*end])
{
printf("%d removed from the queue\n",theQueue[*begin]);
theQueue[*begin] = 0;
*begin = 0;
*end = -1;
}
else
{
printf("%d removed from queue\n",theQueue[*begin]);
theQueue[*begin] = 0;
*begin = ((*begin) + 1) % MAXSIZE;
}
return *begin;
}