-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
57 lines (44 loc) · 1.08 KB
/
queue.c
File metadata and controls
57 lines (44 loc) · 1.08 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct data_pkt{
unsigned int id : 16;
int data_len;
char *data;
} data_pkt;
typedef struct node {
data_pkt val;
struct node *next;
} node_t;
extern void enqueue(node_t **head, data_pkt val) {
node_t *new_node = malloc(sizeof(node_t));
if (!new_node) return;
data_pkt new_pkt = {val.id, val.data_len, strdup(val.data)};
new_node->val = new_pkt;
new_node->next = *head;
*head = new_node;
}
extern data_pkt dequeue(node_t **head) {
node_t *current, *prev = NULL;
data_pkt retval = {-1, 0, ""};
if (*head == NULL) return retval;
current = *head;
while (current->next != NULL) {
prev = current;
current = current->next;
}
retval = current->val;
free(current);
if (prev)
prev->next = NULL;
else
*head = NULL;
return retval;
}
extern void print_list(node_t *head) {
node_t *current = head;
while (current != NULL) {
printf("%s\n", current->val.data);
current = current->next;
}
}