-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
101 lines (90 loc) · 1.7 KB
/
Copy pathqueue.c
File metadata and controls
101 lines (90 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "queue.h"
tQueue *createQueue(int* errorCode)
{
tQueue *q = (tQueue*)malloc(sizeof(tNode));
if (q == NULL)
{
(*errorCode) = OUT_OF_MEMORY;
return NULL;
}
q->front = q->rear = NULL;
q->amount = 0;
return q;
}
bool checkIfEmpty(tQueue *q, int* errorCode)
{
if (q == NULL)
{
(*errorCode) = QUEUE_DOESNT_EXIST;
return NULL;
}
else if (q->front == NULL)
{
return true;
}
return false;
}
bool checkIfFull()
{
if (malloc(sizeof(tNode)) == NULL)
return true;
return false;
}
void enQueue(tQueue *q, user_data x, int* errorCode)
{
if (!checkIfFull())
{
tNode *temp = (tNode*)malloc(sizeof(tNode));
temp->data = x;
temp->next = NULL;
q->amount++;
if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
else
{
(*errorCode) = OUT_OF_MEMORY;
}
}
void deQueue(tQueue *q, int* errorCode)
{
if (q->front == NULL)
{
(*errorCode) = QUEUE_EMPTY;
return;
}
tNode *temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
q->amount--;
}
int getData(tQueue *q, int* errorCode)
{
if (q->front == NULL)
(*errorCode) = QUEUE_EMPTY;
return q->front->data;
}
void freeQueue(tQueue *q)
{
tNode *temp = q->front->next;
tNode *current = temp;
while (temp != NULL)
{
temp = current->next;
free(temp);
temp = current;
}
free(temp);
free(current);
free(q);
}