-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.cpp
105 lines (94 loc) · 1.46 KB
/
Queue.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
using namespace std;
struct Node
{
int info;
struct Node *pNext;
};
typedef struct Node NODE;
struct Queue
{
NODE *pHead;
NODE *pTail;
};
typedef struct Queue QUEUE;
void CreateQueue(QUEUE &q)
{
q.pHead = q.pTail = NULL;
}
int IsEmpty(QUEUE q)
{
if (q.pHead == NULL)
return 1;
return 0;
}
NODE *CreateNode(int x)
{
NODE *p = new NODE;
if (p == NULL)
return NULL;
p->info = x;
p->pNext = NULL;
return p;
}
void EnQueue(QUEUE &q, NODE *p)
{
if (IsEmpty(q) == true)
q.pHead = q.pTail = p;
else
{
q.pTail->pNext = p;
q.pTail = p;
}
}
int DeQueue(QUEUE &q)
{
if (IsEmpty(q) == true)
return 0;
NODE *p = q.pHead;
int x = p->info;
q.pHead = q.pHead->pNext;
delete p;
return x;
}
int Front(QUEUE q)
{
if (IsEmpty(q) == true)
return 0;
return q.pHead->info;
}
int Size(QUEUE q)
{
int count = 0;
NODE *p = q.pHead;
while (p != NULL)
{
count++;
p = p->pNext;
}
return count;
}
void PrintQueue(QUEUE q)
{
while (IsEmpty(q) == false)
{
cout << DeQueue(q) << " ";
}
}
int main()
{
QUEUE q;
CreateQueue(q);
NODE *p = CreateNode(1);
EnQueue(q, p);
p = CreateNode(2);
EnQueue(q, p);
p = CreateNode(3);
EnQueue(q, p);
p = CreateNode(4);
EnQueue(q, p);
p = CreateNode(5);
EnQueue(q, p);
PrintQueue(q);
return 0;
}