-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_queue.c
More file actions
68 lines (50 loc) · 1.28 KB
/
Copy pathlink_queue.c
File metadata and controls
68 lines (50 loc) · 1.28 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
//
// Created by anany on 11/12/2017.
//
#include <stdio.h>
#include <stdlib.h>
typedef struct queue {
struct queue *next;
int value;
} Queue;
typedef struct link_queue {
Queue *head;
Queue *tail;
int length;
} LinkQueue;
LinkQueue *createLinkQueue() {
Queue *queue = malloc(sizeof(Queue));
queue->next = NULL;
queue->value = 0;
LinkQueue *linkQueue = malloc(sizeof(LinkQueue));
linkQueue->head = queue;
linkQueue->tail = queue;
linkQueue->length = 0;
return linkQueue;
}
int insert(LinkQueue *linkQueue, int value) {
Queue *tailLinkQueue = linkQueue->tail;
Queue *newQueue = malloc(sizeof(Queue));
newQueue->value = value;
newQueue->next = NULL;
if (linkQueue->length == 0) { // 空队列
linkQueue->head = newQueue;
} else {
tailLinkQueue->next = newQueue;
}
linkQueue->tail = newQueue;
linkQueue->length++;
return 1;
}
int delete(LinkQueue *linkQueue, int *value) {
if (linkQueue->length == 0) {
return 0;
}
Queue *headQueue = linkQueue->head; // 头节点
*value = headQueue->value;
Queue *nextQueue = headQueue->next; // 头节点的下一个节点
linkQueue->head = nextQueue;
linkQueue->length--;
free(headQueue);
return 1;
}