-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
49 lines (39 loc) · 992 Bytes
/
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
#include "queue.h"
#include <stdlib.h>
#include <string.h>
void initializeQueue(Queue *queue) { queue->front = queue->rear = NULL; }
void enqueue(Queue *queue, const char *path) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->path =
(char *)malloc(strlen(path) + 1); // +1 for the null terminator
strcpy(newNode->path, path);
newNode->next = NULL;
if (queue->rear == NULL) {
queue->front = queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
}
char *dequeue(Queue *queue) {
if (queue->front == NULL) {
return NULL;
}
Node *temp = queue->front;
char *path = temp->path;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
}
free(temp);
return path;
}
void freeQueue(Queue *queue) {
while (queue->front != NULL) {
Node *temp = queue->front;
queue->front = queue->front->next;
free(temp->path);
free(temp);
}
queue->rear = NULL;
}