Lab Manual Queue
Lab Manual Queue
The following program demonstrates how we can implement a queue using array in
C++:
// C++ Program to implement a queue using array
#include <iostream>
using namespace std;
rear++;
arr[rear] = val;
}
return ans;
}
// Enqueueing elements
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.display();
// Dequeueing elements
cout << "\nDequeueing elements:" << endl;
cout << "Dequeued element: " << q.dequeue() << endl;
cout << "Dequeued element: " << q.dequeue() << endl;
q.display();
return 0;
}
Output
After Enqueueing:
Front element: 1
Rear element: 3
Queue: 1 2 3
Queue: 1 2 3 4 5
Dequeueing elements:
Dequeued element: 1
Dequeued element: 2
After Dequeueing:
Front element: 3
Rear element: 6
Queue: 3 4 5 6
// enqueue operation
void enqueue(int val)
{
if (this->isFull()) {
printf("Queue Overflow!\n");
return;
}
rear = (rear + 1) % MAX_SIZE;
arr[rear] = val;
}
// dequeue operation
void dequeue()
{
if (this->isEmpty()) {
printf("Queue Underflow!\n");
return;
}
front = (front + 1) % MAX_SIZE;
}
// peek function
int peek()
{
if (this->isEmpty()) {
printf("Queue is Empty!\n");
return -1;
}
return arr[(front + 1) % MAX_SIZE];
}
// driver code
int main()
{
Queue q;
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.dequeue();
q.dequeue();
q.enqueue(123);
q.print();
return 0;
}
Output
Queue Overflow!
Queue Overflow!
123
#include <iostream>
using namespace std;
public:
// Constructor initializes an empty queue
Queue()
: front(nullptr)
, rear(nullptr)
{
}
// Main function
int main()
{
// Create a queue
Queue q;
// Enqueue elements into the queue
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
// Output the front element
cout << "Front element is: " << q.peek() << endl;
// Dequeue the front element
q.dequeue();
// Output the new front element
cout << "Front element is: " << q.peek() << endl;
// Dequeue the remaining elements
q.dequeue();
q.dequeue();
// Check if the queue is empty and output the result
cout << "Queue is empty: " << q.isEmpty() << endl;
return 0;
}
Output
Front element is: 10
Front element is: 20
Queue is empty: 1
Time and Space Complexity
The time complexity of the implementation of queue using linked list is O(1).
The space complexity of the implementation of queue using linked list is O(n),
where n is number of the nodes in the linked list.
Application of the Linked List Queue
It can applies in operating system uses the queues for job scheduling.
It can be used in web servers for the handling incoming requests in the order.
It can be used for the buffering data streams into the media players.
It can applies on the inter-process communication for the facilitate asynchronous
data exchange between the processes.
Conclusion
Implementing the queue using Linked list in the C++ offers the advantages of the
dynamic memory usage. It can allowing the queue to the expand based on the needs of
the application. This approach can particularly useful in the scenarios where the
maximum size of the queue is not known in advance.
Queue Reversal
Given a Queue Q containing N elements. The task is to reverse the Queue. Your task
is to complete the function rev(), that reverses the N elements of the queue.
Input:
6
4 3 1 10 2 6
Output:
6 2 10 1 3 4
Explanation:
After reversing the given elements of the queue , the
resultant queue will be 6 2 10 1 3 4.
class Solution
{
public:
queue<int> rev(queue<int> q)
{
// add code here.
stack<int> s;
while(!q.empty())
{
s.push(q.front());
q.pop();
}
while(!s.empty())
{
q.push(s.top());
s.pop();
}
return q;
}
};
Input : Q = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
k = 5
Output : Q = [50, 40, 30, 20, 10, 60, 70, 80, 90, 100]
Solution - The idea is to use an auxiliary stack and follow these steps to solve the problem -
// Dequeue the first k elements of the queue and push them onto a deque
for (int i = 0; i < k; i++) {
d.push_front(q.front());
q.pop();
}
// Pop the elements from the deque and enqueue them back into the queue
while (!d.empty()) {
q.push(d.front());
d.pop_front();
}
// Dequeue the remaining elements from the queue and enqueue them back into the queue
for (int i = 0; i < q.size() - k; i++) {
q.push(q.front());
q.pop();
}
return q;
}
};
Expected Time Complexity : O(N)
Expected Auxiliary Space : O(K)
Rotate Deque By K
Given a Deque deq of size N containing non-negative integers and a positive integer K, the task is to
rotate elements of the Deque by K places in a circular fashion. There will be two rotations which you
have to implement depending on the type rotating query. Below is the description of rotating type
and value of K for which you have to rotate in circular way:
Type-1. right_Rotate_Deq_ByK(): this function should rotate deque by K places in a
clockwise fashion.
Type-2. left_Rotate_Deq_ByK(): this function should rotate deque by K places in an anti-
clockwise fashion.
Example 1:
Input:
6
1 2 3 4 5 6
1 2
Output:
5 6 1 2 3 4
Explanation:
The dequeue is 1 2 3 4 5 6.
The query type is 1 and k is 2. So, we
need to right rotate dequeue by 2 times.
In 1 right rotation we get 6 1 2 3 4 5.
In another we get 5 6 1 2 3 4.
Problem: Given a stack data structure with push and pop operations, the task is to implement a
queue using instances of stack data structure and operations on them.
Solution: A queue can be implemented using two stacks. Let the queue to be implemented
be q and stacks used to implement q are stack1 and stack2 respectively.
The queue q can be implemented in two ways: either enQueue operation costly or deQueue
operation costly.
deQueue(q)
1) If stack1 is empty then print an error
2) Pop an item from stack1 and return it
Here time complexity will be O(1)
class StackQueue{
private:
// These are STL stacks ( http://goo.gl/LxlRZQ )
stack<int> s1;
stack<int> s2;
public:
void push(int);
int pop();
}; */
// Return top of s1
int x = s1.top();
s1.pop();
return x;
}
Implementing Stack using Queue
Problem: Given a Queue data structure that supports standard operations like enqueue() and
dequeue(). We need to implement a Stack data structure using only instances of Queue and
queue operations allowed on the instances.
This problem is just the opposite of the problem described in the previous post of implementing a
queue using stacks. Similar to the previous problem, a stack can also be implemented using two
queues. Let stack to be implemented be 's' and queues used to implement be 'q1' and 'q2'.
Stack 's' can be implemented in two ways: either push or pop operation costly.
By making push operation costly: This method makes sure that newly entered element is
always at the front of 'q1', so that pop operation just dequeues from 'q1'. The queue, 'q2' is
used to put every new element at front of 'q1'.
pop(s)
1) Dequeue an item from q1 and return it.
class QueueStack{
private:
queue<int> q1;
queue<int> q2;
public:
void push(int);
int pop();
};
*/
q1.push(x);
while(q2.empty()==false)
{
q1.push(q2.front());
q2.pop();
}
}
void generatePrintBinary(int n)
{
for(int i=1;i<=n;i++){
string str="";
int temp=i;
while(temp){
if(temp&1){str=to_string(1)+str;}
else{str=to_string(0)+str;}
temp=temp>>1;
}
cout<<str<<endl;
}
}
// Driver code
int main()
{
int n = 10;
// Function call
generatePrintBinary(n);
return 0;
}
Output-
1
10
11
100
101
110
111
1000
1001
1010
Time Complexity: O(N*logN),N for traversing from 1 to N and logN for decimal to
binary conversion
Auxiliary Space: O(1)
Generate Binary Numbers from 1 to n using the queue:
Follow the given steps to solve the problem:
Create an empty queue of strings
Enqueue the first binary number “1” to the queue.
Now run a loop for generating and printing n binary numbers.
o Dequeue and Print the front of queue.
o Append “0” at the end of front item and enqueue it.
o Append “1” at the end of front item and enqueue it.
// Driver code
int main()
{
int n = 10;
// Function call
generatePrintBinary(n);
return 0;
}
Output
1
10
11
100
101
110
111
1000
1001
1010
Time Complexity: O(N)
Auxiliary Space: O(N) as extra space is required in this method