-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdemo_link_queue_1.c
More file actions
68 lines (59 loc) · 1.24 KB
/
demo_link_queue_1.c
File metadata and controls
68 lines (59 loc) · 1.24 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
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include "demo_link_queue_1.h"
void create_queue(List *list) {
list = (List *)malloc(sizeof(List));
list->head = NULL;
list->tail = NULL;
list->count = 0;
}
bool enqueue(List *list, int data) {
LinkQueue *p = (LinkQueue *)malloc(sizeof(LinkQueue));
p->key = data;
p->next = NULL;
if (list->head == NULL) {
list->head = p;
} else {
list->tail->next = p;
}
list->tail = p;
list->count++;
return true;
}
int dequeue(List *list) {
if (list == NULL || list->head == NULL) {
return false;
}
LinkQueue *temp = list->head;
int key = temp->key;
list->head = temp->next;
list->count--;
if (list->head == NULL) {
list->tail == NULL;
}
free(temp);
return key;
}
void destory(List *list) {
if (list == NULL || list->count == 0) return;
while(!list->count == 0) {
dequeue(list);
}
free(list);
}
void test_link_queue() {
List l;
create_queue(&l);
int i;
for (i = 0 ; i < 8; i++) {
enqueue(&l, i);
}
for (i = 0 ; i < 8; i++) {
printf("dequeue val: %d\n", dequeue(&l));
}
destory(&l);
}
int main () {
test_link_queue();
return 0;
}