-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue (2).cpp
More file actions
68 lines (55 loc) · 1.24 KB
/
queue (2).cpp
File metadata and controls
68 lines (55 loc) · 1.24 KB
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
#include <iostream>
#include <queue>
using namespace std;
template <typename T>
void cut(queue<T> &q, int n, const T& item)
{
if (n < 1 || n > q.size() + 1)
{
cout << "Invalid value of n. We cannot perform the operation." << endl;
return;
}
queue<T> temp;
// Move the first n-1 elements to temp
for (int i = 1; i < n; ++i)
{
temp.push(q.front());
q.pop();
}
// Insert the item at position n
temp.push(item);
// Move the remaining elements back to the original queue
while (!q.empty())
{
temp.push(q.front());
q.pop();
}
// Reassign temp to the original queue
q = temp;
}
int main()
{
queue<int> myQueue;
cout << "Enter five int values to enqueue into the queue:" << endl;
for (int i = 0; i < 5; ++i)
{
int value;
cin >> value;
myQueue.push(value);
}
int n;
int item;
cout << "Enter the value of n: ";
cin >> n;
cout << "Enter the value of item: ";
cin >> item;
cut(myQueue, n, item);
cout << "Queue after cut operation: ";
while (!myQueue.empty())
{
cout << myQueue.front() << " ";
myQueue.pop();
}
cout << endl;
return 0;
}