-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
99 lines (87 loc) · 1.54 KB
/
queue.h
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
typedef struct listNode{
struct listNode *next;
char *value;
}listNode;
typedef struct list{
listNode *head;
listNode *tail;
unsigned int len;
}list;
static list *queue;
#define listLength(l) ((l)->len)
#define listFirst(l) ((l)->head)
#define listLast(l) ((l)->tail)
list *listCreate(void)
{
struct list *list;
if ((list = malloc(sizeof(*list))) == NULL)
return NULL;
list->head = list->tail = NULL;
list->len = 0;
return list;
}
void listRelease(list *list)
{
unsigned int len;
listNode *current, *next;
current = list->head;
len = list->len;
while (len--)
{
next = current->next;
free(current->value);
free(current);
current = next;
}
free(list);
}
list *listAddNodeTail(list *list, char *value)
{
listNode *node;
if ((node = malloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (list->len == 0)
{
list->head = list->tail = node;
node->next = NULL;
}
else
{
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
list->len++;
return list;
}
void listDelNodeHead(list *list)
{
listNode *oldHead;
oldHead = list->head;
list->head = oldHead->next;
if (list->head == NULL)
list->tail = NULL;
free(oldHead->value);
free(oldHead);
list->len--;
}
void printList(list *list)
{
listNode *current;
unsigned int len;
current = list->head;
len = list->len;
printf("the queue contains %d nodes:\n",len);
while ( len--)
{
printf("%s\n",current->value);
current = current->next;
}
}
#endif