-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathQueue(Template).cpp
80 lines (74 loc) · 991 Bytes
/
Queue(Template).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
#include <iostream>
using namespace std;
template<typename T> class Queue
{
int sz;
T *A;
int fronts,rears;
public:
Queue()
{
sz=0;
fronts=-1,rears=-1;
A=new T[sz];
}
Queue(int S)
{
sz=S;
fronts=-1,rears=-1;
A=new T[S];
}
void enqueue(T el)
{
if((fronts+1)%sz==rears||rears==-1&&fronts==sz-1)
{
printf("Queue Overflow\n");
}
else
{
fronts=(fronts+1)%sz;
A[fronts]=el;
}
}
void dequeue()
{
if(rears==fronts)
{
printf("Queue Underflow\n");
}
else
{
rears=(rears+1)%sz;
}
}
T front()
{
if(fronts!=rears)
{
return A[fronts];
}
else
{
cout<<"Nothing to show\n";
return -1;
}
}
};
int main()
{
Queue<int> Q(3);
Q.enqueue(4);
Q.enqueue(45);
cout<<Q.front()<<endl;
Q.enqueue(23);
Q.enqueue(53);
Q.dequeue();
cout<<Q.front()<<endl;
Q.dequeue();
Q.dequeue();
Q.dequeue();
Q.dequeue();
Q.enqueue(33);
cout<<Q.front()<<endl;
return 0;
}