-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.cpp
153 lines (129 loc) · 2.33 KB
/
queue.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
/* C++ program to implement queue
* Queue is kind of data structure in which, we insert data from rear end and delete from front end(for array implementation), and it is FIFI ds
* Written By: Ramchandra Shahi Thakuri
* Date:
*/
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
template <class T>
class queue
{
private:
T a[10];
int f;
int r;
int SIZE;
public:
queue(int n)
{
f = 0;
r = -1;
SIZE = n;
}
void insert_rear();
void delete_front();
void display();
};
template <class T> void queue <T> :: insert_rear()
{
T item;
if(r == SIZE - 1)
{
cout << "\nQueue overflow, item can not be inserted\n";
return;
}
cout << "\nEnter item to be inserted: ";
cin >> item;
a[++r] = item;
return;
}
template <class T> void queue <T> :: delete_front()
{
if(f > r)
{
cout << "\nQueue is underflow, can not delete item\n";
return;
}
cout << "\nItem deleted is " << a[f++] << endl;
return;
}
template <class T> void queue <T> :: display()
{
if(f > r)
{
cout << "\nQueue is underflow, not item to display\n";
return;
}
cout << "\nQueue item are: \n";
for(int i = f; i < SIZE; i++)
{
cout << a[i] << endl;
}
return;
}
int main()
{
system("clear");
int n;
cout << "Enter size of queue: ";
cin >> n;
//creating queue for double and int
queue <int> q1(n);
queue <double> q2(n);
int ch1, ch2;
cout << "\n1. Integer queue\n2. Double queue\nEnter your choice: ";
cin >> ch1;
while(1)
{
switch(ch1)
{
case 1:
while(1)
{
cout << "\n1.Insert Queue\n2. Delete Queue\n3. Display\n4. Quit\nEnter your choice: ";
cin >> ch2;
switch(ch2)
{
case 1:
q1.insert_rear();
break;
case 2:
q1.delete_front();
break;
case 3:
q1.display();
break;
default:
cout << "\nThank you!\n";
return 0;
}
}
case 2:
while(1)
{
cout << "\n1.Insert Queue\n2. Delete Queue\n3. Display\n4. Quit\nEnter your choice: ";
cin >> ch2;
switch(ch2)
{
case 1:
q2.insert_rear();
break;
case 2:
q2.delete_front();
break;
case 3:
q2.display();
break;
default:
cout << "\nThank you!\n";
return 0;
}
}
default:
cout << "\nInvalid choice\nPlease enter valid choice\n";
}
}
return 0;
}