-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue by SLL.cpp
88 lines (84 loc) · 1.24 KB
/
Queue by SLL.cpp
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
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct qnode{
int item;
qnode *next;
};
struct queue{
qnode *front;
qnode *rear;
};
// khoi tao
queue *khoi_tao()
{
queue *q;
q=(queue *)malloc(sizeof(queue));
if(q==NULL) return NULL;
else{
q->front=NULL;
q->rear=NULL;
}
return q;
}
queue *isFull(queue *q){
if(q==NULL) printf("hang doi full");
return q;
}
int isEmpty(queue *q){
return ((q->rear==NULL)&&(q->front==NULL));
}
// them phan tu vao hang doi
queue *enqueue (queue *q, int x){
qnode *node;
node=(qnode *)malloc(sizeof(qnode));
if(isEmpty(q))
{node->item=x;
q->front=node;
q->rear=node;
node->next=NULL;
}
else
{
node->item=x;
q->rear->next=node;
q->rear=node;
node->next=NULL;
}
return q;
}
queue *dequeue(queue *q)
{
qnode *node;
if(q->front==NULL && q->rear==NULL)
isEmpty(q);
else{
node=q->front;
q->front=q->front->next;
if(q->front==NULL)
q->rear=NULL;
node->next=NULL;
}
return q;
}
void print(queue *q){
printf("danh sach phan tu trong hang doi la:\n");
qnode *node;
int m;
node=q->front;
do{
m=node->item;
printf("%2d",m);
node=node->next;
}
while(node!=NULL);
}
int main()
{
queue *hd;
hd=khoi_tao();
hd=enqueue(hd,1);
hd=enqueue(hd,3);
print(hd);
return 0;
}