Given an empty queue and an array of elements, we need to perform the following two operations.
1. Filling a Queue
Filling a queue refers to the process of adding array elements to it. In a queue, elements are added to the rear or back of the queue.
- Insert Elements: You begin by inserting elements into the queue, typically one by one using an operation such as enqueue or push (depending on the language or context).
- Order: Each element is added at the back of the queue, and the order in which they are added is maintained. The first element inserted will be at the front of the queue when it is dequeued (removed).
2. Emptying a Queue
Emptying a queue means removing elements from it. In a queue, elements are removed from the front of the queue, adhering to the FIFO rule.
- Remove Elements: The elements are removed from the front of the queue using an operation like dequeue or pop.
- Order of Removal: The first element that was added to the queue is the first to be removed. This continues until the queue is empty.
Examples:
Input: arr[] = [1, 2, 3, 4, 5]
Output: 1 2 3 4 5
Explanation: Enqueue the elements of the array into a queue in the same order, then dequeue and print the elements, resulting in the same order as the input: 1 2 3 4 5
.
Input: arr[] = [1, 6, 43, 1, 2, 0, 5]
Output: 1 6 43 1 2 0 5
Explanation: Enqueue the elements of the array into a queue in the same order, then dequeue and print the elements, resulting in the same order as the input: 1 6 43 1 2 0 5.
The approach uses a queue, which follows the First-In-First-Out (FIFO) principle. Elements are added to the back and removed from the front in the same order they were added. The first element inserted is the first one to be removed, ensuring they are processed in the order they arrive.
C++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
queue<int> fillQ(const vector<int> &v)
{
int n = v.size();
queue<int> q;
for (int i = 0; i < n; i++)
{
q.push(v[i]);
}
return q;
}
void emptyQ(queue<int> &q)
{
while (!q.empty())
{
cout << q.front() << " ";
q.pop();
}
cout << endl;
}
int main()
{
vector<int> arr = {1, 2, 3, 4, 5};
queue<int> q;
q = fillQ(arr);
emptyQ(q);
return 0;
}
Java
// Java program to demonstrate queue operations
import java.util.LinkedList;
import java.util.Queue;
import java.util.List;
public class Main {
static Queue<Integer> fillQ(List<Integer> v) {
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < v.size(); i++) {
q.add(v.get(i));
}
return q;
}
static void emptyQ(Queue<Integer> q) {
while (!q.isEmpty()) {
System.out.print(q.poll() + " ");
}
System.out.println();
}
public static void main(String[] args) {
List<Integer> arr = List.of(1, 2, 3, 4, 5);
Queue<Integer> q = fillQ(arr);
emptyQ(q);
}
}
Python
# Python program to demonstrate queue operations
from queue import Queue
def fillQ(v):
q = Queue()
for item in v:
q.put(item)
return q
def emptyQ(q):
while not q.empty():
print(q.get(), end=' ')
print() # Newline
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5]
q = fillQ(arr)
emptyQ(q)
C#
// C# program to demonstrate queue operations
using System;
using System.Collections.Generic;
class Program {
static Queue<int> FillQ(List<int> v) {
Queue<int> q = new Queue<int>();
foreach (var item in v) {
q.Enqueue(item);
}
return q;
}
static void EmptyQ(Queue<int> q) {
while (q.Count > 0) {
Console.Write(q.Dequeue() + " ");
}
Console.WriteLine();
}
static void Main() {
List<int> arr = new List<int> { 1, 2, 3, 4, 5 };
Queue<int> q = FillQ(arr);
EmptyQ(q);
}
}
JavaScript
// JavaScript program to demonstrate queue operations
function fillQ(arr) {
let q = [];
for (let i = 0; i < arr.length; i++) {
q.push(arr[i]);
}
return q;
}
function emptyQ(q) {
while (q.length > 0) {
console.log(q.shift(), "");
}
console.log();
}
const arr = [1, 2, 3, 4, 5];
const q = fillQ(arr);
emptyQ(q);
Time Complexity: O(n)
Auxiliary Space: O(n)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem