How to remove a specific element from Queue
Last Updated :
06 Feb, 2023
Given a queue q[] and an integer K, the task is to define a method to remove a specific element from the queue q[]. If there are multiple occurrences of element K, then, remove the first one from the queue q[].
Examples:
Input: q[] = {10, 20, 30, 40, 50, 60}, K = 30
Output: {10, 20, 40, 50, 60}
Explanation: After removal of 30, the queue becomes {10, 20, 40, 50, 60}.
Input: q[] = {1, 2, 3, 3}, K = 3
Output: {1, 2, 3}
Explanation: After removal of the first occurrence of 3, the queue becomes {1, 2, 3}.
Approach: The idea is to create a temporary queue ref[] and store all the elements in it, until K is found. Then, remove K from the original queue q[], and insert the remaining elements back into the queue q[]. Follow the steps below to solve the problem:
- Initialize a helper queue ref to store the elements of the queue q[] temporarily.
- Initialize variables s as the size of the queue q[] and cnt as 0 to store the count of numbers pushed into the helper queue ref[].
- Iterate in a while loop till the queue q[] is not empty and the front of the queue is not equal to the desired element K:
- If the queue q[] is empty, then the element K is not present in the queue q[], so print “element not found!!” and perform the following steps:
- Else, the element is found, so Dequeue an element from the queue q[] and performs the following steps:
- Initialize the variable k as s-cnt-1 to mark the number of elements required to dequeue from the queue q[] and enqueue back again to the queue q[].
- Iterate in a while loop till K is greater than 0 and perform the following steps:
- After performing the above steps, print the elements of the queue q[].
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void remove ( int t, queue< int >& q)
{
queue< int > ref;
int s = q.size();
int cnt = 0;
while (q.front() != t and !q.empty()) {
ref.push(q.front());
q.pop();
cnt++;
}
if (q.empty()) {
cout << "element not found!!" << endl;
while (!ref.empty()) {
q.push(ref.front());
ref.pop();
}
}
else {
q.pop();
while (!ref.empty()) {
q.push(ref.front());
ref.pop();
}
int k = s - cnt - 1;
while (k--) {
int p = q.front();
q.pop();
q.push(p);
}
}
}
void print(queue< int > qr)
{
while (!qr.empty()) {
cout << qr.front() << " " ;
qr.pop();
}
cout << endl;
}
int main()
{
queue< int > q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
q.push(50);
q.push(60);
print(q);
remove (39, q);
print(q);
remove (30, q);
print(q);
return 0;
}
|
Java
import java.util.*;
class GFG{
static Queue<Integer> q;
static void remove( int t)
{
Queue<Integer> ref = new LinkedList<>();
int s = q.size();
int cnt = 0 ;
while (!q.isEmpty() && q.peek() != t) {
ref.add(q.peek());
q.remove();
cnt++;
}
if (q.isEmpty()) {
System.out.print( "element not found!!" + "\n" );
while (!ref.isEmpty()) {
q.add(ref.peek());
ref.remove();
}
}
else {
q.remove();
while (!ref.isEmpty()) {
q.add(ref.peek());
ref.remove();
}
int k = s - cnt - 1 ;
while (k-- > 0 ) {
int p = q.peek();
q.remove();
q.add(p);
}
}
}
static void print()
{
Queue<Integer> qr = new LinkedList<>(q);
while (!qr.isEmpty()) {
System.out.print(qr.peek()+ " " );
qr.remove();
}
System.out.println();
}
public static void main(String[] args)
{
q = new LinkedList<>();
q.add( 10 );
q.add( 20 );
q.add( 30 );
q.add( 40 );
q.add( 50 );
q.add( 60 );
print();
remove( 39 );
print();
remove( 30 );
print();
}
}
|
Python3
import collections
def remove(t,queue):
q2 = collections.deque()
s = len (queue)
cnt = 0
while queue and queue[ 0 ] ! = t:
q2.append(queue.popleft())
cnt + = 1
if len (queue) = = 0 :
print ( "element not found!!" )
while q2:
queue.append(q2.popleft())
else :
queue.popleft()
while q2:
queue.append(q2.popleft())
k = s - cnt - 1
while k:
queue.append(queue.popleft())
k - = 1
def printQ(queue):
tmp = queue.copy()
while tmp:
print (tmp.popleft(),end = ' ' )
print ()
queue = collections.deque()
queue.append( 10 )
queue.append( 20 )
queue.append( 30 )
queue.append( 40 )
queue.append( 50 )
queue.append( 60 )
printQ(queue)
remove( 39 , queue)
printQ(queue)
remove( 30 , queue)
printQ(queue)
|
C#
using System;
using System.Collections;
public class GFG{
static Queue q = new Queue();
static void remove_( int t)
{
Queue reff = new Queue();
int s = q.Count;
int cnt = 0;
while (( int )q.Count != 0 && ( int )q.Peek() != t) {
reff.Enqueue(q.Peek());
q.Dequeue();
cnt++;
}
if (q.Count == 0) {
Console.WriteLine( "element not found!!" );
while (reff.Count != 0) {
q.Enqueue(reff.Peek());
reff.Dequeue();
}
}
else {
q.Dequeue();
while (reff.Count != 0) {
q.Enqueue(reff.Peek());
reff.Dequeue();
}
int k = s - cnt - 1;
while (k-- >0) {
int p = ( int )q.Peek();
q.Dequeue();
q.Enqueue(p);
}
}
}
static void print()
{
Queue qr = (Queue)q.Clone();
while (qr.Count != 0) {
Console.Write(qr.Peek()+ " " );
qr.Dequeue();
}
Console.WriteLine();
}
static public void Main (){
q.Enqueue(10);
q.Enqueue(20);
q.Enqueue(30);
q.Enqueue(40);
q.Enqueue(50);
q.Enqueue(60);
print();
remove_(39);
print();
remove_(30);
print();
}
}
|
Javascript
function remove(t, queue) {
let q2 = [];
let s = queue.length;
let cnt = 0;
while (queue.length && queue[0] !== t) {
q2.push(queue.shift());
cnt += 1;
}
if (queue.length === 0) {
console.log( "element not found!!" );
while (q2.length) {
queue.push(q2.shift());
}
}
else {
queue.shift();
while (q2.length) {
queue.push(q2.shift());
}
let k = s - cnt - 1;
while (k) {
queue.push(queue.shift());
k -= 1;
}
}
}
function printQ(queue) {
let tmp = [...queue];
while (tmp.length) {
process.stdout.write(tmp.shift() + ' ' );
}
console.log();
}
let queue = [];
queue.push(10);
queue.push(20);
queue.push(30);
queue.push(40);
queue.push(50);
queue.push(60);
printQ(queue);
remove(39, queue);
printQ(queue);
remove(30, queue);
printQ(queue);
|
Output
10 20 30 40 50 60
element not found!!
10 20 30 40 50 60
10 20 40 50 60
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...