-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain5.c
67 lines (56 loc) · 1.08 KB
/
main5.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
57
58
59
60
61
62
63
64
65
66
67
//
// Created by ge on 2023/12/11.
//
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *initQueue() {
Node *Q = (Node *) malloc(sizeof(Node));
Q->data = 0;
Q->next = NULL;
return Q;
}
void *enQueue(Node *Q, int data) {
Node *q = Q;
Node *node = (Node *) malloc(sizeof(Node));
node->data = data;
for (int i = 0; i < Q->data; i++) {
q = q->next;
}
node->next = q->next;
q->next = node;
Q->data++;
}
int isEmpty(Node * Q){
return Q->data == 0 || Q->next == NULL;
}
int deQueue(Node *Q){
if (isEmpty(Q)){
return -1;
}
Node *node = Q->next;
int data = node->data;
Q->next = node->next;
free(node);
Q->data--;
return data;
}
void printQueue(Node *S) {
Node *node = S->next;
while (node) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main() {
Node * Q = initQueue();
enQueue(Q,1);
enQueue(Q,2);
enQueue(Q,3);
printQueue(Q);
printf("%d\n", deQueue(Q));
}