-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12b- Queues template
160 lines (132 loc) · 2.07 KB
/
12b- Queues template
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
153
154
155
156
157
158
159
160
/**************************************
13b) Write a C++ program to create a class called QUEUE with member functions to add an
element and to delete an element from the queue. Using these member functions, implement a
queue of integers and doubles. Demonstrate the operations by displaying the contents of the queue
after every operation.
****************************************/
#include <iostream>
#define QSIZE 6
using namespace std;
int i;
template <class X>
class que
{
int r=-1;
int f=0;
X q[QSIZE];
public:
void add(X);
void Delete();
void display();
};
template <class X>
void que<X>::add(X item)
{
if(r==QSIZE-1)
cout<<"Cannot Insert. que is full"<<endl;
else
{
q[++r] = item;
}
}
template <class X>
void que<X>::Delete()
{
if(f>r)
cout<<"que is Empty."<<endl;
else
{
cout<<"Item deleted is "<<q[f]<<endl;
f++;
if(f>r)
{
f=0;
r=-1;
}
}
}
template <class X>
void que<X>::display()
{
if(f>r)
{
cout<<"The que is empty"<<endl;
}
cout<<"The contents of the que are: "<<endl;
for(i=f; i<=r; i++)
{
cout<<q[i]<<endl;
}
}
void integerchoice(que<int> qi)
{
int item;
int choice;
while(1)
{
cout<<"1. Add integer"<<endl;
cout<<"2. Delete integer"<<endl;
cout<<"3. Display"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Item? ";
cin>>item;
qi.add(item);
break;
case 2:
qi.Delete();
break;
case 3:
qi.display();
}
}
}
void doublechoice(que<double> qd)
{
double item;
int choice;
while(1)
{
cout<<"1. Add double"<<endl;
cout<<"2. Delete double"<<endl;
cout<<"3. Display"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Item? ";
cin>>item;
qd.add(item);
break;
case 2:
qd.Delete();
break;
case 3:
qd.display();
}
}
}
int main()
{
que<int> qi;
que<double> qd;
int choice;
while(1)
{
cout<<"Enter a choice: "<<endl;
cout<<"1. que of Integers."<<endl;
cout<<"2. que of doubles."<<endl;
cin>>choice;
switch(choice)
{
case 1:
integerchoice(qi);
break;
case 2:
doublechoice(qd);
break;
}
}
}