-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ques-8.cpp
152 lines (152 loc) · 1.84 KB
/
Ques-8.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
QUESTION-8:
Perform Queues operations using Circular Array implementation. Use Templates.
SOLUTION:
*/
#include<iostream>
using namespace std;
const int size=5;
int flag=0;
template<class t>
class CQ
{
static const int size=5;
t queue[size];
int front,rear;
public:
CQ()
{
front=rear=-1;
}
void insert(t n)
{
if((rear+1)%size!=front)
{
rear=(rear+1)%size;
queue[rear]=n;
if(front<0)
front=0;
}
else
{
cout<<"Queue overflow element not inserted:\n";
flag=-1;
//fflush(stdin);
}
}
t remove()
{
t a;
if(empty()!=-1)
{
if((front==rear)&&(front>=0))
{
a=queue[front];
front=rear=-1;
}
else if(front!=size-1)
{
a=queue[front++];
}
else
{
front=0;
a=queue[front];
}
return a;
}
else
{
cout<<"Queue underflow:\n";
return -1;
}
}
int empty()
{
if(front==-1)
return -1;
else
return 0;
}
void display()
{
if(empty()==-1)
{
cout<<"Empty queue:\n";
}
else
{
int r,f;
r=rear;
f=front;
while((f+1)%size!=(r+1)%size)
{
cout<<queue[f]<<endl;
f=(f+1)%size;
}
cout<<queue[f]<<endl;
/*cout<<"the values in the original array:\n";
for(int i=0;i<size;i++)
{
cout<<queue[i]<<" ";
}
cout<<endl;*/
}
}
};
int main()
{
cout << "\n\t ~~~~~~~~~~~~~~~~~~~~~~~Practical 8~~~~~~~~~~~~~~~~~~~~\n\t\t\t\t \n";
CQ<char> q1;
char ch;
char n;
flag=0;
do
{
cout<<"Enter 'y' if want to enter any element:\n";
cin>>ch;
if(ch=='y')
{
cout<<"Enter the element:\n";
cin>>n;
q1.insert(n);
if(flag==-1)
break;
}
}while(ch=='y');
cout<<"The resulting queue:\n";
q1.display();
flag=0;
if(q1.empty()!=-1)
{
do
{
cout<<"Enter 'y' if want to remove any element:\n";
cin>>ch;
if(ch=='y')
{
n=q1.remove();
if(n==-1)
break;
}
}while(ch=='y');
cout<<"The resulting queue:\n";
q1.display();
do
{
cout<<"Enter 'y' if want to enter any element:\n";
cin>>ch;
if(ch=='y')
{
cout<<"Enter the element:\n";
cin>>n;
q1.insert(n);
if(flag==-1)
break;
}
}while(ch=='y');
cout<<"The resulting queue:\n";
q1.display();
}
return 0;
}