-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
60 lines (57 loc) · 1.21 KB
/
queue.c
File metadata and controls
60 lines (57 loc) · 1.21 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
#include "queue.h"
#include <stdlib.h>
#include <stdio.h>
/**
* Enqueue an element in the queue
* @param q pointer to the queue
* @param element pointer to the element to enqueue
*/
void enqueue (Queue* q, void* element)
{
QueueElement *queueEl = (QueueElement *) malloc(sizeof(QueueElement));
queueEl->item = element;
queueEl->next = NULL;
if (q->tail == NULL)
{
q->tail = queueEl;
q->head = queueEl;
}
else
{
q->tail->next = queueEl;
q->tail = queueEl;
}
}
/**
* Dequeue an element, i.e. remove and return the head of the queue
* @param q pointer to the queue
*/
void* dequeue (Queue* q)
{
if (q->head == NULL)
{
return NULL;
}
else
{
QueueElement* element = q->head;
q->head = element->next;
return element->item;
}
}
int main(int argc, char const *argv[])
{
Queue q;
char string1[50] = "(char*) malloc(5*sizeof(char));";
char string2[90] = "(char*) malloc(10*sizeof(char))";
char test[50] = "blop";
// printf("%s\n", test);
enqueue(&q, (void*) string1);
enqueue(&q, (void*) string2);
enqueue(&q, (void*)test);
printf("%s\n", (char*) dequeue(&q));
printf("%s\n", (char*) dequeue(&q));
printf("%s\n", (char*) dequeue(&q));
// char* blop = (char*) dequeue(&q);
return 0;
}