📜  如何从队列中删除特定元素

📅  最后修改于: 2022-05-13 01:57:00.746000             🧑  作者: Mango

如何从队列中删除特定元素

给定一个队列q[]和一个整数K,任务是定义一个 从队列q[]中删除特定元素的方法。如果元素K 出现多次,则从队列q[] 中删除第一个。

例子:

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。

做法:思路是创建一个临时队列ref[] ,将所有元素存放在里面,直到找到K。然后,从原队列q[]中移除K ,并将剩余的元素重新插入队列q[]。请按照以下步骤解决问题:

  • 初始化一个辅助队列ref来临时存储队列q[]的元素。
  • 将变量s初始化为队列q[]的大小,将cnt初始化为0以存储推入辅助队列ref[] 的数字计数。
  • 在 while 循环中迭代,直到队列q[]不为空并且队列的前面不等于所需的元素K:
    • 将队列q[]的前元素入队到队列ref[] 中。
    • 从队列q[] 中取出一个元素。
    • cnt的值增加1。
  • 如果队列q[]为空,则队列 q[] 中不存在元素K 因此打印“找不到元素!!”并执行以下步骤:
    • 在 while 循环中迭代,直到队列ref[]不为空:
      • 将队列ref[]的前元素加入队列q[]。
      • 从队列ref[] 中取出一个元素。
  • 否则,找到元素,因此从队列q[]中出列一个元素并执行以下步骤:
    • 在 while 循环中迭代,直到队列ref[]不为空:
      • 将队列ref[]的前元素加入队列q[]。
      • 从队列ref[] 中取出一个元素。
  • 将变量k初始化为s-cnt-1以标记从队列q[]出队并再次入队到队列 q[] 所需的元素数量
  • 在while循环中迭代直到K大于0并执行以下步骤:
    • K的值减去1。
    • 将队列q[]的前元素入队到队列q[] 中。
    • 从队列q[] 中取出一个元素。
  • 执行完上述步骤后,打印队列q[]的元素。

下面是上述方法的实现。

C++
// C++ program for the above approach.
#include 
using namespace std;
 
// Function to remove an element from
// the queue
void remove(int t, queue& q)
{
 
    // Helper queue to store the elements
    // temporarily.
    queue ref;
    int s = q.size();
    int cnt = 0;
 
    // Finding the value to be removed
    while (q.front() != t and !q.empty()) {
        ref.push(q.front());
        q.pop();
        cnt++;
    }
 
    // If element is not found
    if (q.empty()) {
        cout << "element not found!!" << endl;
        while (!ref.empty()) {
 
            // Pushing all the elements back into q
            q.push(ref.front());
            ref.pop();
        }
    }
 
    // If element is found
    else {
        q.pop();
        while (!ref.empty()) {
 
            // Pushing all the elements back into q
            q.push(ref.front());
            ref.pop();
        }
        int k = s - cnt - 1;
        while (k--) {
 
            // Pushing elements from front of q to its back
            int p = q.front();
            q.pop();
            q.push(p);
        }
    }
}
 
// Function to print all the elements
// of the queue.
void print(queue qr)
{
    while (!qr.empty()) {
        cout << qr.front() << " ";
        qr.pop();
    }
    cout << endl;
}
 
// Driver Code
int main()
{
    queue q;
 
    // Pushing into the queue
    q.push(10);
    q.push(20);
    q.push(30);
    q.push(40);
    q.push(50);
    q.push(60);
    print(q);
 
    // Removing 39 from the queue
    remove(39, q);
    print(q);
 
    // Removing 30 from the queue
    remove(30, q);
    print(q);
    return 0;
}


Java
// Java program for the above approach.
 
import java.util.*;
 
class GFG{
 
// Function to remove an element from
// the queue
static Queue q;
static void remove(int t)
{
 
    // Helper queue to store the elements
    // temporarily.
    Queue ref = new LinkedList<>();
    int s = q.size();
    int cnt = 0;
 
    // Finding the value to be removed
    while (!q.isEmpty() && q.peek() != t) {
        ref.add(q.peek());
        q.remove();
        cnt++;
    }
 
    // If element is not found
    if (q.isEmpty()) {
        System.out.print("element not found!!" +"\n");
        while (!ref.isEmpty()) {
 
            // Pushing all the elements back into q
            q.add(ref.peek());
            ref.remove();
        }
    }
 
    // If element is found
    else {
        q.remove();
        while (!ref.isEmpty()) {
 
            // Pushing all the elements back into q
            q.add(ref.peek());
            ref.remove();
        }
        int k = s - cnt - 1;
        while (k-- >0) {
 
            // Pushing elements from front of q to its back
            int p = q.peek();
            q.remove();
            q.add(p);
        }
    }
}
 
// Function to print all the elements
// of the queue.
static void print()
{
    Queue qr = new LinkedList<>(q);
    while (!qr.isEmpty()) {
        System.out.print(qr.peek()+ " ");
        qr.remove();
    }
 
    System.out.println();
}
 
// Driver Code
public static void main(String[] args)
{
    q = new LinkedList<>();
 
    // Pushing into the queue
    q.add(10);
    q.add(20);
    q.add(30);
    q.add(40);
    q.add(50);
    q.add(60);
    print();
 
    // Removing 39 from the queue
    remove(39);
    print();
 
    // Removing 30 from the queue
    remove(30);
    print();
}
}
 
// This code is contributed by 29AjayKumar


C#
// C# program for the above approach.
using System;
using System.Collections;
public class GFG{
     
      // Function to remove an element from
// the queue
static Queue q = new Queue();
static void remove_(int t)
{
 
    // Helper queue to store the elements
    // temporarily.
    Queue reff = new Queue();
    int s = q.Count;
    int cnt = 0;
 
    // Finding the value to be removed
    while ((int)q.Count != 0 && (int)q.Peek() != t) {
       
        reff.Enqueue(q.Peek());
        q.Dequeue();
        cnt++;
    }
 
    // If element is not found
    if (q.Count == 0) {
        Console.WriteLine("element not found!!");
           
        while (reff.Count != 0) {
             
            // Pushing all the elements back into q
            q.Enqueue(reff.Peek());
            reff.Dequeue();
        }
    }
 
    // If element is found
    else {
        q.Dequeue();
        while (reff.Count != 0) {
 
            // Pushing all the elements back into q
            q.Enqueue(reff.Peek());
            reff.Dequeue();
        }
        int k = s - cnt - 1;
        while (k-- >0) {
 
            // Pushing elements from front of q to its back
            int p = (int)q.Peek();
            q.Dequeue();
            q.Enqueue(p);
        }
    }
}
 
// Function to print all the elements
// of the queue.
static void print()
{
    Queue qr = (Queue)q.Clone();
    while (qr.Count != 0) {
        Console.Write(qr.Peek()+ " ");
        qr.Dequeue();
    }
 
    Console.WriteLine();
}
 
// Driver Code
static public void Main (){
 
    // Pushing into the queue
    q.Enqueue(10);
    q.Enqueue(20);
    q.Enqueue(30);
    q.Enqueue(40);
    q.Enqueue(50);
    q.Enqueue(60);
   
    print();
 
    // Removing 39 from the queue
    remove_(39);
    print();
 
    // Removing 30 from the queue
    remove_(30);
    print();
}
}
 
// This code is contributed by Dharanendra L V.


输出
10 20 30 40 50 60 
element not found!!
10 20 30 40 50 60 
10 20 40 50 60 

时间复杂度: O(N)
辅助空间: O(N)