-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
56 lines (52 loc) · 1.08 KB
/
queue.cpp
File metadata and controls
56 lines (52 loc) · 1.08 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
#include <iostream>
#include <queue>
using namespace std;
template <typename T>
void n2front(queue<T> &q, int n)
{
if (n < 1 || n > q.size())
{
cout << "Invalid value of n. Cannot perform the operation." << endl;
return;
}
queue<T> temp;
for (int i = 1; i < n; ++i)
{
temp.push(q.front());
q.pop();
}
T zerothElement = q.front();
q.pop();
T nthElement = q.front();
q.pop();
q.push(zerothElement);
temp.push(nthElement);
while (!temp.empty())
{
q.push(temp.front());
temp.pop();
}
}
int main()
{
queue<int> myQueue;
cout << "Enter five integer values to enqueue into the queue:" << endl;
for (int i = 0; i < 5; ++i)
{
int value;
cin >> value;
myQueue.push(value);
}
int n;
cout << "Enter the value of n: ";
cin >> n;
n2front(myQueue, n);
cout << "Queue after n2front operation: ";
while (!myQueue.empty())
{
cout << myQueue.front() << " ";
myQueue.pop();
}
cout << endl;
return 0;
}