Design Front Middle Back Queue using STL
Design a data structure that supports the following operations in queue efficiently:
- push__front(x): Insert an element at the front of the queue.
- push__middle(x): Inserts element at the middle of the queue.
- push__back(x): Inserts element at the back of the queue.
- pop__front() Removes the front element of the queue and returns it. If the queue is empty, returns -1.
- pop__middle(): Removes the middle element of the queue and returns it. If the queue is empty, returns -1.
- pop__back():Removes the back element of the queue and returns it. If the queue is empty, returns -1.
Examples:
Operations |
Queue |
Return |
push__front(4) |
4 |
_ |
push__back(2) |
4, 2 |
_ |
push__middle(1) |
4, 1, 2 |
_ |
pop_front() |
1, 2 |
4 |
pop__middle() |
2 |
1 |
pop__front() |
_ |
2 |
pop__front() |
_ |
-1 |
Deque-based Approach: The problem can be solved using two deque. The idea is to use two deques. Operation at the back of the queue is to be done at the end of the second deque, and operation at the middle is to be done at the end of the first deque. Follow the steps below to solve the problem:
- If the size of the first deque is greater than the size of the second deque, then remove the end element of the first deque and add it to the front of the second deque.
- If the size of the second deque exceeds the size of the first deque by 1, then remove the front element of the second deque and push it at the end of the first deque.
- If the size of the first deque is greater than the second deque, then remove the back element from the first deque and insert it into the second deque.
- push__front(x): Insert an element x at the front of the first deque using push_front().
- push__back(x): Insert an element x at the end of the second deque using push_back()
- push__middle(x): Insert the element x at the end of the first deque using push_back().
- pop__front(): Remove the front element of the first deque if the size of deque is greater than 0 using pop_front().
- pop__back(): Remove the end element of the second deque if the size of deque greater than 0 using pop_back().
- pop__middle(): Remove the end element of the first deque if the size of deque greater than using pop_back().
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Queue {
deque< int > first, second;
void equalizeSizedeque1deque2()
{
if (first.size() <= second.size())
return ;
second.push_front(first.back());
first.pop_back();
}
void equalizeSizedeque2deque1()
{
if (second.size() <= first.size() + 1)
return ;
first.push_back(second.front());
second.pop_front();
}
public :
void push__front( int val)
{
first.push_front(val);
equalizeSizedeque1deque2();
}
void push__middle( int val)
{
first.push_back(val);
equalizeSizedeque1deque2();
}
void push__back( int val)
{
second.push_back(val);
equalizeSizedeque2deque1();
}
int pop__front()
{
if (first.empty() && second.empty())
return -1;
int ans;
if (first.empty()) {
ans = second.front();
second.pop_front();
}
else {
ans = first.front();
first.pop_front();
equalizeSizedeque2deque1();
}
return ans;
}
int pop__middle()
{
if (first.empty() && second.empty())
return -1;
int ans;
if (first.size() == second.size()) {
ans = first.back();
first.pop_back();
}
else {
ans = second.front();
second.pop_front();
}
return ans;
}
int pop__back()
{
if (first.empty() && second.empty())
return -1;
int ans = second.back();
second.pop_back();
equalizeSizedeque1deque2();
return ans;
}
};
int main()
{
Queue q;
q.push__front(1);
q.push__back(2);
q.push__middle(3);
cout << q.pop__middle() << " " ;
cout << q.pop__back() << " " ;
cout << q.pop__front() << " " ;
return 0;
}
|
Time Complexity Analysis:
push__front(x) |
pop__front() |
push__back(x) |
pop__back() |
push__middle(x) |
pop__middle() |
O(1) |
O(1) |
O(1) |
O(1) |
O(1) |
O(1) |
List-based Approach: Follow the steps below to solve the problem:
- push__front(x): Insert an element x at the front of the list using push_front().
- push__back(x): Insert an element x at the end of the second list using push_back()
- push__middle(x): Traverse the list using advance() and then insert the element at mid position of the list using insert()
- pop__front(): Remove the front element of the list if the size of list greater than 0 using pop_front(), otherwise return -1.
- pop__back(): Remove the last element of the list if the size of list greater than 0 using pop_back(), otherwise return -1.
- pop__middle(): If the size of the list greater than 0, then iterate to the middle element of the list using advance() and then erase the element at that position using erase(). Otherwise, return -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Queue {
list< int > l;
public :
void push__front( int val)
{
l.push_front(val);
}
void push__middle( int val)
{
auto itr = l.begin();
advance(itr, l.size() / 2);
l.insert(itr, val);
}
void push__back( int val)
{
l.push_back(val);
}
int pop__front()
{
int val = -1;
if (l.size()) {
val = l.front();
l.pop_front();
}
return val;
}
int pop__middle()
{
int val = -1;
if (l.size()) {
auto itr = l.begin();
advance(itr, (l.size() - 1) / 2);
val = *itr;
l.erase(itr);
}
return val;
}
int pop__back()
{
int val = -1;
if (l.size()) {
val = l.back();
l.pop_back();
}
return val;
}
};
int main()
{
Queue q;
q.push__front(1);
q.push__back(2);
q.push__middle(3);
cout << q.pop__middle() << " " ;
cout << q.pop__back() << " " ;
cout << q.pop__front() << " " ;
return 0;
}
|
Time Complexity Analysis:
push__front(x) |
pop__front() |
push__back(x) |
pop__back() |
push__middle(x) |
pop__middle() |
O(1) |
O(1) |
O(1) |
O(1) |
O(N) |
O(N) |
Doubly linked list-based Approach: The problem can also be solved using a doubly-linked list without using STL by storing the address of the head and last node. Follow the steps below to solve the problem:
- push__front(x):
- Allocate space for storing the data value x and store the address in the current node pointer
- Insert the element x by linking the current node between head node and head->next node
- Increment the capacity by one
- push__back(x):
- Allocate space for storing the data value x and store the address in the current node pointer
- Insert the element x by linking the current node between the last node and last->previous node
- Increment the capacity by one
- push__middle(x):
- Allocate space for storing the data value x and store the address in the current node pointer
- Initialize a temp pointer of type node
- Reach the middle element of the doubly linked list by doing temp=temp->next half of current capacity times
- Now Insert the element x between temp and temp->next by relinking nodes
- Increment the capacity by one
- pop__front()
- If the capacity of size is less than 1 then return -1
- Otherwise, delete the first node between the head and head->next nodes by relinking nodes
- Decrement the capacity by one
- Return value of the deleted element
- pop__back():
- If the capacity is less than 1 then return -1
- Otherwise, delete the end node between the last and last->previous nodes by relinking nodes
- Decrement the capacity by one
- Return value of the deleted element
- pop__middle():
- Initialize a temp pointer of type node
- Reach the middle element of the doubly linked list by doing temp=temp->next half of current capacity times
- Now delete temp node between temp->previous and temp->next nodes by relinking nodes
- Decrement the capacity by one
- Return value of the deleted element
Last Updated :
04 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...