-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListQueue.cpp
107 lines (104 loc) · 1.64 KB
/
LinkedListQueue.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
#include <iostream>
using namespace std;
template <class t>
struct node {
t data;
node <t> *next;
node (){
next = 0;
}
node (t item){
data = item;
next = 0;
}
};
template <class t>
class LinkedQueue {
node <t> *front , *rear ;
int count ;
public :
LinkedQueue(){
count = 0;
front=rear = 0;
}
bool empty(){
return count==0;
}
bool append (t item){
node <t> *temp = new node <t> (item);
if (temp==NULL)return 0;
if (empty())front=rear=temp;
else{
rear->next = temp;
rear =temp;
}
count++;
return 1;
}
bool serve (){
if (empty())return 0;
node <t> *temp = front;
front = front->next;
if (front == 0)
rear =0;
delete temp;
count--;
return 1;
}
bool size (){
return count;
}
bool retrive (t &item){
if (empty())return 0;
item = front->data;
return 1;
}
~LinkedQueue (){
while (!empty())
serve();
}
void operator = (LinkedQueue <t>&outer){
while (!empty()){
serve();
}
t load;
LinkedQueue <t> temp ;
while (!outer.empty()){
outer.retrive(load);
append(load);
temp.append(load);
outer.serve();
}
while (!temp.empty()){
temp.retrive(load);
outer.append(load);
temp.serve();
}
}
void print (){
node <t> *test = front ;
while (test != 0 ){
cout<<test->data<<" | ";
test = test->next;
}
cout<<endl;
}
};
int main(int argc, char *argv[])
{
LinkedQueue <int> q1 ,q2;
q1.append(10);
q2.append(66);
q1.append(122);
q1.append(334);
q2.append(54);
q2.append(12);
q1.append(122);
q2.append(40);
q2=q1;
q2.print();
q2.append(40);
q1.print();
q2.print();
return 0;
}