-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueuBasedOnArray.cpp
104 lines (95 loc) · 1.7 KB
/
QueuBasedOnArray.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
#include <iostream>
using namespace std;
const int maxsize = 100;
template <class t>
class queue {
int front ;
int rear;
t arrQueue [maxsize];
int count ;
public :
queue (){
front =0;
rear = maxsize-1;
count = 0;
}
bool fullQueu() {
return (count==maxsize);
}
int Size (){
return count;
}
bool appendQueue (t item){
if (fullQueu())
return 0;
rear = (rear + 1)%maxsize;
arrQueue[rear] = item;
count++;
return 1;
}
bool isEmpty (){
return (count==0);
}
bool serveQueue(){
if (isEmpty())
return 0;
front = (front+1)%maxsize;
count--;
return 1;
}
bool Retrive (t &item){
if (isEmpty())
return 0;
item = arrQueue[front];
return 1;
}
bool printQueue (){
cout<<"[ ";
for (int i=front ; i!=rear ; i=(i+1)%maxsize)
cout << arrQueue[i]<<" << " ;
cout<<arrQueue[rear]<<" ]"<<endl;
}
};
bool delRevRepItemQue(queue<int>&q1) ;
int main()
{
queue <int> q1 ,q2;
q1.appendQueue(1);
q1.appendQueue(2);
q1.appendQueue(3);
q1.appendQueue(4);
q1.appendQueue(5);
q1.appendQueue(6);
q1.appendQueue(7);
q1.appendQueue(8);
q1.appendQueue(9);
//q2.print();
q1.printQueue();
cout<<"------------"<<q1.Size()<<endl;
delRevRepItemQue(q1);
q1.printQueue();
cout<<"------------"<<q1.Size()<<endl;
return 0;
}
bool delRevRepItemQue(queue<int>&q1)
{
if (q1.isEmpty())return 0;
queue<int> TQ;
int x;
while (!q1.isEmpty()) {
for (int i=0; i<q1.Size()-1; i++) {
q1.Retrive(x);
q1.serveQueue();
q1.appendQueue(x);
}
q1.Retrive(x);
TQ.appendQueue(x);
q1.serveQueue();
}
while (!TQ.isEmpty()){
TQ.Retrive(x);
q1.appendQueue(x);
TQ.serveQueue();
}
return 1;
}