Implement a Queue as mentioned:
An opertaion Z is of 2 Types.
- 1 (opertion of this type means: delete the element from the queue and insert that element again in the queue)
- 0 (opertion of this type means you need to delete in queue)
Input Format
- First line tell the nos. of element in the queue
- Second line tell the nos. of operation
- third line depicts the elements
- fourth line depicts the operation
Constraints
- Initial nos. of element is always more than 0 & less than 50.
- nos. of operation is always more than 0 & less than 10.
Output Format
print the element of queue after operation(space sperated)
Sample Input 0
8 4 2 1 4 8 9 7 9 3 1 0 1 0
Sample Output 0
9 7 9 3 2 4
Solution
nos = int(input())
op = int(input())
queue = list(map(int, input().split()))
opList = list(map(int, input().split()))
# sauravhathi
for i in opList:
if i == 1:
queue.append(queue.pop(0))
else:
queue.pop(0)
print(*queue, sep=" ")
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
queue<int> q;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
q.push(x);
}
// sauravhathi
for (int i = 0; i < m; i++) {
int x;
cin >> x;
if (x == 1) {
int y = q.front();
q.pop();
q.push(y);
} else {
q.pop();
}
}
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
cout << endl;
return 0;
}
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
q.add(sc.nextInt());
}
// sauravhathi
for (int i = 0; i < m; i++) {
int x = sc.nextInt();
if (x == 1) {
int y = q.peek();
q.remove();
q.add(y);
} else {
q.remove();
}
}
while (!q.isEmpty()) {
System.out.print(q.peek() + " ");
q.remove();
}
System.out.println();
}
}
Happy Learning – If you require any further information, feel free to contact me.