Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <queue>
- using namespace std;
- queue<int> reverseK(queue<int>& q, int k)
- {
- queue<int> resultq;
- stack<int> st;
- int i = 0;
- while (q.size() != 0)
- {
- if (i < k)
- {
- st.push(q.front());
- q.pop();
- i++;
- }
- else if (i == k)
- {
- while (!st.empty())
- {
- resultq.push(st.top());
- st.pop();
- }
- resultq.push(q.front());
- q.pop();
- i++;
- }
- else
- {
- resultq.push(q.front());
- q.pop();
- }
- }
- return resultq;
- }
- int main()
- {
- queue<int> q1;
- int n;
- cout << "Enter number of queue elements: ";
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- int el;
- cin >> el;
- q1.push(el);
- }
- int k;
- do
- {
- cout << "Enter k: ";
- cin >> k;
- }
- while (k < 0 || k > n);
- queue<int> q2 = reverseK(q1, k);
- while (q2.size() != 0)
- {
- cout << q2.front() << " ";
- q2.pop();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement