📜  队列操作

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

队列操作

给定N个整数,任务是将这些元素插入队列中。此外,给定M个整数,任务是找到队列中每个数字的频率。

例子:

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

方法:给定的问题可以通过使用一个简单的队列来解决。思路是将N个整数一个一个的插入到队列中,然后检查M个整数出现的频率。将创建单独的函数来执行这两种操作。请按照以下步骤解决问题。

  • 创建一个队列并将所有给定的整数推送到队列中。
  • 在将所有元素推入队列后,创建另一个函数来一个一个地计算给定M个元素的频率。
  • 使用变量cntFrequency来跟踪当前元素的频率。
  • 从队列中弹出所有元素直到它的大小,每次检查队列中的当前元素是否等于我们正在检查频率的元素。
    – 如果两者相等,则将 cntFrequency增加 1
    ——否则,什么都不做。
  • 将当前元素推回队列,以便下一个案例队列不会受到干扰。
  • 打印找到的每个元素的频率。

下面是上述方法的实现:

Java
// Java Program to implement above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
        // Declaring Queue
        Queue q = new LinkedList<>();
        int N = 8;
        int[] a = new int[] { 1, 2, 3, 4, 5, 2, 3, 1 };
        int M = 5;
        int[] b = new int[] { 1, 3, 2, 9, 10 };
 
        // Invoking object of Geeks class
        Geeks obj = new Geeks();
 
        for (int i = 0; i < N; i++) {
            // calling insert()
            // to add elements in queue
            obj.insert(q, a[i]);
        }
 
        for (int i = 0; i < M; i++) {
            // calling findFrequency()
            int f = obj.findFrequency(q, b[i]);
            if (f != 0) {
                // variable f
                // will have final frequency
                System.out.print(f + " ");
            }
            else {
                System.out.print("-1"
                                 + " ");
            }
        }
    }
}
 
// Helper class Geeks to implement
// insert() and findFrequency()
class Geeks {
 
    // Function to insert
    // element into the queue
    static void insert(Queue q, int k)
    {
        // adding N integers into the Queue
        q.add(k);
    }
 
    // Function to find frequency of an element
    // return the frequency of k
    static int findFrequency(Queue q, int k)
    {
        // to count frequency of elements
        int cntFrequency = 0;
 
        // storing size of queue in a variable
        int size = q.size();
 
        // running loop until size becomes zero
        while (size-- != 0) {
 
            // storing and deleting
            // first element from queue
            int x = q.poll();
 
            // comparing if it's equal to integer K
            // that belongs to M
            if (x == k) {
                // increament count
                cntFrequency++;
            }
            // add element back to queue because
            // we also want N integers
            q.add(x);
        }
        // return the count
        return cntFrequency;
    }
}


Python3
# Python Program to implement above approach
import collections
 
# Helper class Geeks to implement
# insert() and findFrequency()
class Geeks:
 
    # Function to insert
    # element into the queue
    def insert(self, q, k):
       
        # adding N integers into the Queue
        q.append(k)
 
    # Function to find frequency of an element
    # return the frequency of k
    def findFrequency(self, q, k):
       
        # to count frequency of elements
        cntFrequency = 0
 
        # storing size of queue in a variable
        size = len(q)
 
        # running loop until size becomes zero
        while (size):
            size = size - 1
             
            # storing and deleting
            # first element from queue
            x = q.popleft()
 
            # comparing if it's equal to integer K
            # that belongs to M
            if (x == k):
               
                # increament count
                cntFrequency += 1
                 
            # add element back to queue because
            # we also want N integers
            q.append(x)
 
        # return the count
        return cntFrequency
 
# Declaring Queue
q = collections.deque()
N = 8
a = [1, 2, 3, 4, 5, 2, 3, 1]
M = 5
b = [1, 3, 2, 9, 10]
 
# Invoking object of Geeks class
obj = Geeks()
 
for i in range(N):
 
    # calling insert()
    # to add elements in queue
    obj.insert(q, a[i])
 
for i in range(M):
 
    # calling findFrequency()
    f = obj.findFrequency(q, b[i])
    if (f != 0):
       
        # variable f
        # will have final frequency
        print(f, end=" ")
    else:
        print("-1", end=" ")
print(" ")
 
# This code is contributed by gfgking.


C#
// C# Program to implement above approach
using System;
using System.Collections.Generic;
 
public class GFG {
   
  // Helper class Geeks to implement
// insert() and findFrequency()
public class Geeks {
 
    // Function to insert
    // element into the queue
   public void insert(Queue q, int k)
    {
        // adding N integers into the Queue
        q.Enqueue(k);
    }
 
    // Function to find frequency of an element
    // return the frequency of k
    public int findFrequency(Queue q, int k)
    {
        // to count frequency of elements
        int cntFrequency = 0;
 
        // storing size of queue in a variable
        int size = q.Count;
 
        // running loop until size becomes zero
        while (size-- != 0) {
 
            // storing and deleting
            // first element from queue
            int x = q.Peek();
            q.Dequeue();
 
            // comparing if it's equal to integer K
            // that belongs to M
            if (x == k) {
                // increament count
                cntFrequency++;
            }
            // add element back to queue because
            // we also want N integers
            q.Enqueue(x);
        }
        // return the count
        return cntFrequency;
    }
}
 
    // Driver code
    public static void Main()
    {
       
        // Declaring Queue
        Queue q = new Queue();
        int N = 8;
        int[] a = new int[] { 1, 2, 3, 4, 5, 2, 3, 1 };
        int M = 5;
        int[] b = new int[] { 1, 3, 2, 9, 10 };
 
        // Invoking object of Geeks class
        Geeks obj = new Geeks();
 
        for (int i = 0; i < N; i++)
        {
           
            // calling insert()
            // to add elements in queue
            obj.insert(q, a[i]);
        }
 
        for (int i = 0; i < M; i++)
        {
           
            // calling findFrequency()
            int f = obj.findFrequency(q, b[i]);
            if (f != 0)
            {
               
                // variable f
                // will have final frequency
                Console.Write(f + " ");
            }
            else {
                Console.Write("-1"
                                 + " ");
            }
        }
    }
}
 
// This code is contributed by SURENDRA_GANGWAR.


输出
2 2 2 -1 -1 

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