-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
59 lines (58 loc) · 1.04 KB
/
queue.c
File metadata and controls
59 lines (58 loc) · 1.04 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
/*Lungu Andrei Daniel-313CD*/
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
//standard queue functions
TQueue *createQueue()
{
TQueue *q = calloc(1, sizeof(TQueue));
return q;
}
TList *createNode(void *elem)
{
TList *list = calloc(1, sizeof(TList));
list->elem = elem;
return list;
}
int isQueueEmpty(TQueue *q)
{
return q->front == NULL;
}
void enqueue(TQueue *q, void *elem)
{
if (elem == NULL)
return;
if (q->front == NULL)
{
q->front = q->rear = createNode(elem);
}
else
{
TList *list = createNode(elem);
q->rear->next = list;
q->rear = list;
}
}
void *dequeue(TQueue *q)
{
if (q->front == NULL)
return NULL;
TList *aux = q->front;
q->front = q->front->next;
void *result = aux->elem;
free(aux);
return result;
}
void freeQueue(TQueue *q)
{
if (q == NULL)
return;
TList *aux = q->front;
while (aux)
{
q->front = q->front->next;
free(aux);
aux = q->front;
}
free(q);
}