-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexerc_2_6.c
87 lines (71 loc) · 1.88 KB
/
exerc_2_6.c
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
/*====================================
File name: exerc_2_6.c (or cpp) Date: 2014‐01‐28
Group Number: 3
Members that contributed:
Patrik Bäckström,
John Burchell,
William Granli
Demonstration code: 51498
======================================*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define QUEUE_SIZE 5
int add(int queue[], int number);
void initQue(int array[]);
void printQueue(int *);
int getFirst(int *);
int main(int argc, char *argv[]) {
int queue[QUEUE_SIZE];
initQue(queue);
srand(time(NULL));
int addAmount = QUEUE_SIZE + 1; // Add this amount of entries
while (addAmount) { // Automated for testing purpose
printQueue(queue);
int input = rand() % 100; // or get from user input
0 == add(queue,input) ? printf("\n\t\t# ERROR: Queue full. Cannot add %i.\n", input) : printf("\n\t# Added %i to queue!", input);
addAmount--;
}
printQueue(queue);
int removeAmount = QUEUE_SIZE; // Remove this amount of entries
while (removeAmount) { // Automated for testing purpose
int nextNum = getFirst(queue);
printf("\n\t# Removing first queue element: %i", nextNum);
printQueue(queue);
removeAmount--;
}
return 0;
}
void initQue(int *queue) {
for (int i = 0; i < QUEUE_SIZE; i++) {
*(queue + i) = -1;
}
}
int add(int *queue, int num) {
if (*(queue + QUEUE_SIZE - 1) == -1) { // Instantly checks if queue is full
for (int i = 0; i < QUEUE_SIZE; i++) {
if (*(queue + i) == -1) {
*(queue + i) = num;
break;
}
}
return 1;
} else {
return 0;
}
}
int getFirst(int *queue) {
int nextNum = *queue;
for (int i = 1; i < QUEUE_SIZE; i++) {
*(queue + i - 1) = *(queue + i);
QUEUE_SIZE - 1 == i ? *(queue + i) = -1 : 0;
}
return nextNum;
}
void printQueue(int *queue) {
printf("\n\t# Current queue: ");
for (int i = 0; i < QUEUE_SIZE; i++) {
printf("%d", *(queue + i));
QUEUE_SIZE - 1 == i ? printf("\n") : printf(",");
}
}