-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangdoi.c
85 lines (77 loc) · 1.46 KB
/
hangdoi.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct _node
{
int data;
struct _node *next;
} node;
typedef struct _queue
{
node *front;
node *back;
} queue;
queue *queueInit()
{
queue *q = (queue*) malloc(sizeof(queue));
if(q == NULL)
return NULL;
q->front = NULL;
q->back = NULL;
return q;
}
int queueEmpty(queue *q)
{
return q->front == NULL;
}
void enqueue(queue *q, int x)
{
node *newnode = (node*)malloc(sizeof(node));
newnode->data = x;
newnode->next = NULL;
if(q->front == NULL && q->back == NULL)
{
q->front = newnode;
q->back = newnode;
}
else
{
q->back->next = newnode;
q->back = newnode;
}
}
int dequeue(queue *q)
{
if(queueEmpty(q))
return 0;
node *tempnode;
int x;
if(q->front == q->back)
{
tempnode = q->front;
q->front = NULL;
q->back = NULL;
x = tempnode->data;
free(tempnode);
return x;
}
else
{
tempnode = q->front;
q->front = q->front->next;
x = tempnode->data;
free(tempnode);
return x;
}
}
int main()
{
queue *q = queueInit();
enqueue(q,10);
enqueue(q,30);
enqueue(q,40);
while(!queueEmpty(q))
printf("%d\n",dequeue(q));
printf("\n-----------------------------\n");
return 0;
}