-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
83 lines (67 loc) · 1.32 KB
/
queue.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
#include "queue.h"
#define STORE_FILE "queue.bin"
#define NOTES_MAX_SIZE 1024
Note queue[NOTES_MAX_SIZE];
int queueIndex;
int finshedCount;
void initQueue() {
printf("Init new queue\n");
queueIndex = -1;
finshedCount = 0;
}
void pushNote(Note note){
queue[++queueIndex] = note;
}
Note popNote(){
finshedCount++;
return queue[queueIndex--];
}
Note pullNote(){
return queue[queueIndex];
}
Note getNote(int index){
return queue[queueIndex-index];
}
void printList(){
printf("Notes(%d): \n",queueSize());
int i,c = 0;
for(i = 0;i <= queueIndex;i++){
printf("%3d ", queueIndex - c);
printShortNote(&queue[i]);
c++;
}
}
int queueSize(){
return queueIndex + 1;
}
int loadQueue() {
FILE* f;
f = fopen(STORE_FILE, "r");
if(!f){
printf("Can not load queue\n");
return 0;
}
fread(&queueIndex,sizeof(int),1,f);
fread(&finshedCount,sizeof(int),1,f);
fread(queue,sizeof(Note),queueIndex + 1,f);
fclose(f);
if(queueIndex < -1 || queueIndex >= NOTES_MAX_SIZE)
return 0;
printf("Queue loaded\n");
return 1;
}
void storeQueue() {
FILE* f;
f = fopen(STORE_FILE, "w");
if(!f){
printf("Can not store queue\n");
return;
}
fwrite(&queueIndex,sizeof(int),1,f);
fwrite(&finshedCount,sizeof(int),1,f);
fwrite(queue,sizeof(Note),queueIndex + 1,f);
fclose(f);
}
int getFinishedCount(){
return finshedCount;
}