-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
56 lines (52 loc) · 1.26 KB
/
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
50
51
52
53
54
55
56
#include "queue.h"
void init_queue(int type, node *head, node **tail) {
head->type = type;
head->data = NULL;
head->next = NULL;
*tail = head;
}
void *pop_queue(node *head, node **tail) {
if(head->next == NULL) {
printf("Queue is empty\n");
return NULL;
} else {
node *temp = head->next;
head->next = head->next->next;
if(head->next == NULL)
*tail = head;
return temp->data;
}
}
void push_queue(node *head, node **tail, void *data) {
node *new_node;
new_node = malloc(sizeof(node));
if(new_node == NULL) {
perror("malloc error");
exit(1);
}
if(head->type == 0) {
new_node->data = malloc(sizeof(struct sockaddr_in));
*((struct sockaddr_in *)new_node->data) = *((struct sockaddr_in *)data);
} else {
new_node->data = malloc(sizeof(int));
*((int *)new_node->data) = *((int *)data);
}
new_node->type = head->type;
(*tail)->next = new_node;
new_node->next = NULL;
*tail = new_node;
}
int contains(node *head, void *data) {
node *current = head->next;
while(current != NULL) {
if(head->type == 0) {
if((*((struct sockaddr_in *)current->data)).sin_addr.s_addr == (*((struct sockaddr_in *)data)).sin_addr.s_addr)
return 0;
} else {
if(*(int *)current->data == *(int *)data)
return 0;
}
current = current->next;
}
return 1;
}