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++
// C++ program to implement// the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Create class Queue.class Queue {Â
    // Initialize two deques    deque<int> first, second;Â
    // Function to balance the size of    // first ans second deque    void equalizeSizedeque1deque2()    {Â
        // If size of less than second        if (first.size() <= second.size())            return;Â
        // Insert the last element of        // first deque into second deque        second.push_front(first.back());Â
        // Pop the front element        // of the deque        first.pop_back();    }Â
    // Function to balance the size of    // second and first deque    void equalizeSizedeque2deque1()    {Â
        // if size of second deque deceed        // the first deque by 1        if (second.size() <= first.size() + 1)            return;Â
        // Insert front element of second        // deque into the first        first.push_back(second.front());Â
        // Remove front element of        // second deque        second.pop_front();    }Â
public:    // Function to insert element    // at front of queue    void push__front(int val)    {Â
        // Insert val into first deque        first.push_front(val);Â
        // Balancing the size of second        equalizeSizedeque1deque2();    }Â
    // Function to insert val    // into the middle of queue    void push__middle(int val)    {Â
        // Insert val into first deque        first.push_back(val);Â
        // Balancing the size of        // second deque        equalizeSizedeque1deque2();    }Â
    // Function to insert val    // into back of queue    void push__back(int val)    {Â
        // Insert val into second deque        second.push_back(val);Â
        // Balancing the size of        // second deque        equalizeSizedeque2deque1();    }Â
    // Function to pop front    // element from queue    int pop__front()    {Â
        // If first deque and second        // deque is empty        if (first.empty() && second.empty())            return -1;Â
        int ans;Â
        // If the first deque        // is empty        if (first.empty()) {Â
            // Stores front element            // of second deque            ans = second.front();Â
            // Pop front element of            // second deque            second.pop_front();        }        else {Â
            // Stores front element            // of first deque            ans = first.front();Â
            // Pop front element of            // first deque            first.pop_front();Â
            // Balancing the size of first            equalizeSizedeque2deque1();        }        return ans;    }Â
    // Function to pop middle    // element of queue    int pop__middle()    {Â
        // If both deques are empty        if (first.empty() && second.empty())            return -1;Â
        // Stores mid element        // of queue        int ans;Â
        // If size of both deque is equal        if (first.size() == second.size()) {Â
            // Stores back element            // of first deque            ans = first.back();Â
            // Pop back element of            // first deque            first.pop_back();        }        else {Â
            // Stores front element            // of second deque            ans = second.front();Â
            // Pop front element            // from second deque            second.pop_front();        }        return ans;    }Â
    // Function to remove mid    // element from queue    int pop__back()    {Â
        // If both the deque are empty        if (first.empty() && second.empty())            return -1;Â
        // Stores back element from        // second deque        int ans = second.back();Â
        // Pop back element from        // second deque        second.pop_back();Â
        // Balancing the size of second        equalizeSizedeque1deque2();        return ans;    }};Â
// Driver codeint 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;} |
3 2 1
Â
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++
// C++ program to implement// the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Create structure of queueclass Queue {Â
    list<int> l;Â
public:    // Function to push element    // at front of the queue    void push__front(int val)    {        l.push_front(val);    }Â
    // Function to push element    // at middle of the queue    void push__middle(int val)    {Â
        auto itr = l.begin();Â
        // Traverse the list        advance(itr, l.size() / 2);Â
        // Insert element into        // middle of the list        l.insert(itr, val);    }Â
    // Function to insert element    // at the back of the queue    void push__back(int val)    {        l.push_back(val);    }Â
    // Function to pop element from    // front of the queue    int pop__front()    {Â
        // Stores front element        // of queue        int val = -1;        if (l.size()) {            val = l.front();            l.pop_front();        }        return val;    }Â
    // Function to pop middle element    // of the queue    int pop__middle()    {        int val = -1;        if (l.size()) {            auto itr = l.begin();Â
            // Traverse the list            advance(itr, (l.size() - 1) / 2);            val = *itr;Â
            // Remove mid element            // from queue            l.erase(itr);        }        return val;    }Â
    // Function to pop end    // element of the queue    int pop__back()    {Â
        // Stores back element        // of the queue        int val = -1;Â
        if (l.size()) {            val = l.back();            l.pop_back();        }        return val;    }};Â
// Drivers codeint 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;} |
3 2 1
Â
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
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



