This repository was archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueu_list.cpp
117 lines (108 loc) · 1.33 KB
/
queu_list.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
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
using namespace std;
struct Node
{
int Value;
Node *Next;
};
class queue
{
Node *Head, *Tail;
public:
queue();
~queue();
void push(int );
bool pop(int &);
bool isEmpty();
int size();
void print();
};
queue::queue()
{
Head = new Node;
Head->Next = NULL;
Tail = Head;
}
queue::~queue()
{
Node *t = Head->Next;
Node *p = Head;
delete p;
while (t != NULL)
{
p = t;
t = t->Next;
delete p;
}
}
void queue::push(int item)
{
Node *t = new Node;
t->Value = item;
t->Next = NULL;
Tail->Next = t;
Tail = t;
}
bool queue::pop(int &out)
{
if (isEmpty())
return false;
Node *t = Head->Next;
out = t->Value;
Head->Next = t->Next;
delete t;
return true;
}
bool queue::isEmpty()
{
if (Head->Next == NULL)
return true;
return false;
}
int queue::size()
{
Node *t = Head->Next;
int count = 0;
while (t != NULL)
{
count++;
t = t->Next;
}
return count;
}
void queue::print()
{
Node *t = Head->Next;
while (t != NULL)
{
cout << t->Value << " ";
t = t->Next;
}
cout << endl;
}
int main()
{
queue A;
int a;
A.push(5);
A.push(9);
A.push(1);
A.push(7);
A.print();
A.pop(a);
cout << a << endl;
A.pop(a);
cout << a << endl;
A.print();
cin >> a;
A.push(6);
A.push(12);
A.print();
cin >> a;
A.pop(a);
cout << a << endl;
A.print();
cin >> a;
cout << A.size() << endl;
return 0;
}