Open In App

Output Restricted Queue

Last Updated : 06 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An output-restricted queue is a special case of a double-ended queue where data can be removed from one end(front) but can be inserted from both ends (front and rear). This kind of Queue does not follow FIFO(first in first out):

Output Restricted Queue

Operations on Output Restricted Queue:

Mainly the following three basic operations are performed on output restricted queue:

  • insertRear(): Adds an item at the rear of the queue. 
  • insertFront(): Adds an item at the front of the queue. 
  • deleteFront(): Deletes an item from the front of the queue. 

In addition to the above operations, the following operations are also supported

  • getFront(): Gets the front item from the m queue. 
  • getRear(): Gets the last item from the m queue. 
  • isEmpty(): Checks whether the r queue is empty or not. 
  • isFull(): Checks whether the queue is full or not. 

Below is the implementation of Output restricted queue:

C++




// C++ implementation of Output Restricted
// Queue using circular array
 
#include <iostream>
using namespace std;
 
// Maximum size of array or Output
// Restricted Queue
#define MAX 100
 
// A structure to represent a Output
// Restricted Queue
class Deque {
    int arr[MAX];
    int front;
    int rear;
    int size;
 
public:
    Deque(int size)
    {
        front = -1;
        rear = 0;
        this->size = size;
    }
 
    // Operations on Output Restricted
    // Queue
    void insertfront(int key);
    void insertrear(int key);
    void deletefront();
    bool isFull();
    bool isEmpty();
    int getFront();
    int getRear();
};
 
// Checks whether Output Restricted
// Queue is full or not.
bool Deque::isFull()
{
    return ((front == 0 && rear == size - 1)
            || front == rear + 1);
}
 
// Checks whether Output Restricted
// Queue is empty or not.
bool Deque::isEmpty() { return (front == -1); }
 
// Function to insert element at front
// end of Output Restricted Queue
void Deque::insertfront(int key)
{
 
    // Check whether Deque if  full or not
    if (isFull()) {
        cout << "Overflow\n"
             << endl;
        return;
    }
 
    // If queue is initially empty
    if (front == -1) {
        front = 0;
        rear = 0;
    }
 
    // Front is at first position of queue
    else if (front == 0)
        front = size - 1;
 
    else
 
        // Decrement front end by '1'
        front = front - 1;
 
    // Insert current element into Deque
    arr[front] = key;
}
 
// Function to insert element at rear end
// of Output Restricted Queue
void Deque::insertrear(int key)
{
    if (isFull()) {
        cout << " Overflow\n " << endl;
        return;
    }
 
    // If queue is initially empty
    if (front == -1) {
        front = 0;
        rear = 0;
    }
 
    // Rear is at last position of queue
    else if (rear == size - 1)
        rear = 0;
 
    // Increment rear end by '1'
    else
        rear = rear + 1;
 
    // Insert current element into Deque
    arr[rear] = key;
}
 
// Deletes element at front end of
// Output Restricted Queue
void Deque::deletefront()
{
 
    // Check whether Deque
    // is empty or not
    if (isEmpty()) {
        cout << "Queue Underflow\n"
             << endl;
        return;
    }
 
    // Deque has only one element
    if (front == rear) {
        front = -1;
        rear = -1;
    }
    else
        // Back to initial position
        if (front == size - 1)
        front = 0;
 
    else
 
        // Increment front by '1' to remove
        // current front value from Deque
        front = front + 1;
}
 
// Returns front element of Output
// Restricted Queue
int Deque::getFront()
{
 
    // Check whether Deque
    // is empty or not
    if (isEmpty()) {
        cout << " Underflow\n"
             << endl;
        return -1;
    }
    return arr[front];
}
 
// Function to return rear element of
// Output Restricted Queue
int Deque::getRear()
{
 
    // Check whether Deque
    // is empty or not
    if (isEmpty() || rear < 0) {
        cout << " Underflow\n"
             << endl;
        return -1;
    }
    return arr[rear];
}
 
// Driver code
int main()
{
    Deque dq(5);
 
    // Function calls
    cout << "Inserted element at rear end : 10 \n";
    dq.insertrear(10);
 
    cout << "Inserted element at rear end : 15 \n";
    dq.insertrear(15);
 
    cout << "Inserted element at front end : 5 \n";
    dq.insertfront(5);
 
    cout << "Get rear element : "
         << " " << dq.getRear() << endl;
 
    cout << "Get front element : " << dq.getFront() << endl;
 
    dq.deletefront();
 
    cout << "After delete front element new "
         << "front become : " << dq.getFront() << endl;
    return 0;
}


Java




import java.util.Arrays;
 
class Deque {
    int[] arr;
    int front;
    int rear;
    int size;
 
    public Deque(int size) {
        front = -1;
        rear = 0;
        this.size = size;
        arr = new int[size];
    }
 
    // Operations on Output Restricted Queue
    public void insertfront(int key) {
        if (isFull()) {
            System.out.println("Overflow\n");
            return;
        }
        if (front == -1) {
            front = 0;
            rear = 0;
        }
        else if (front == 0) {
            front = size - 1;
        }
        else {
            front = front - 1;
        }
        arr[front] = key;
    }
    public void insertrear(int key) {
        if (isFull()) {
            System.out.println("Overflow\n");
            return;
        }
        if (front == -1) {
            front = 0;
            rear = 0;
        }
        else if (rear == size - 1) {
            rear = 0;
        }
        else {
            rear = rear + 1;
        }
        arr[rear] = key;
    }
    public void deletefront() {
        if (isEmpty()) {
            System.out.println("Queue Underflow\n");
            return;
        }
        if (front == rear) {
            front = -1;
            rear = -1;
        }
        else if (front == size - 1) {
            front = 0;
        }
        else {
            front = front + 1;
        }
    }
    public boolean isFull() {
        return ((front == 0 && rear == size - 1) || front == rear + 1);
    }
    public boolean isEmpty() { return (front == -1); }
    public int getFront() {
        if (isEmpty()) {
            System.out.println("Underflow\n");
            return -1;
        }
        return arr[front];
    }
    public int getRear() {
        if (isEmpty() || rear < 0) {
            System.out.println("Underflow\n");
            return -1;
        }
        return arr[rear];
    }
     
    public static void main(String[] args) {
        Deque dq = new Deque(5);
     
        // Function calls
        System.out.println("Inserted element at rear end : 10 \n");
        dq.insertrear(10);
     
        System.out.println("Inserted element at rear end : 15 \n");
        dq.insertrear(15);
     
        System.out.println("Inserted element at front end : 5 \n");
        dq.insertfront(5);
     
        System.out.println("Get rear element : " + dq.getRear() + "\n");
     
        System.out.println("Get front element : " + dq.getFront() + "\n");
     
        dq.deletefront();
     
        System.out.println("After delete front element new front become : " + dq.getFront() + "\n");
    }
}


Python3




class Deque:
    # Initialize the deque with a given size
    def __init__(self, size):
        self.arr = [0] * size
        self.front = -1
        self.rear = 0
        self.size = size
 
    # Check if the deque is full
    def is_full(self):
        return self.front == 0 and self.rear == self.size - 1 or self.front == self.rear + 1
 
    # Check if the deque is empty
    def is_empty(self):
        return self.front == -1
 
    # Insert an element at the front of the deque
    def insertfront(self, key):
        if self.is_full():
            print("Overflow\n")
            return
        if self.front == -1:
            self.front = 0
            self.rear = 0
        elif self.front == 0:
            self.front = self.size - 1
        else:
            self.front = self.front - 1
        self.arr[self.front] = key
 
    # Insert an element at the rear of the deque
    def insertrear(self, key):
        if self.is_full():
            print("Overflow\n")
            return
        if self.front == -1:
            self.front = 0
            self.rear = 0
        elif self.rear == self.size - 1:
            self.rear = 0
        else:
            self.rear = self.rear + 1
        self.arr[self.rear] = key
 
    # Delete an element from the front of the deque
    def deletefront(self):
        if self.is_empty():
            print("Queue Underflow\n")
            return
        if self.front == self.rear:
            self.front = -1
            self.rear = -1
        elif self.front == self.size - 1:
            self.front = 0
        else:
            self.front = self.front + 1
 
    # Get the element at the front of the deque
    def get_front(self):
        if self.is_empty():
            print("Underflow\n")
            return -1
        return self.arr[self.front]
 
    # Get the element at the rear of the deque
    def get_rear(self):
        if self.is_empty() or self.rear < 0:
            print("Underflow\n")
            return -1
        return self.arr[self.rear]
 
 
# Example usage
dq = Deque(5)
print("Inserted element at rear end : 10")
dq.insertrear(10)
print("Inserted element at rear end : 15")
dq.insertrear(15)
print("Inserted element at front end : 5")
dq.insertfront(5)
print("Get rear element :", dq.get_rear())
print("Get front element :", dq.get_front())
dq.deletefront()
print("After delete front element new front become :", dq.get_front())
 
# This code is contributed by divyansh2212


Javascript




<script>
 
// javascript implementation of Output Restricted
// Queue using circular array
 
// Maximum size of array or Output
// Restricted Queue
const MAX = 100;
 
// A structure to represent a Output
// Restricted Queue
class Deque {
     
    constructor(size){
        this.arr = new Array(size).fill(0);
        this.front = -1;
        this.rear = 0;
        this.size = size;
    }
 
    // Checks whether Output Restricted
    // Queue is full or not.
    isFull()
    {
        return ((this.front == 0 && this.rear == this.size - 1) || this.front == this.rear + 1);
    }
     
    // Checks whether Output Restricted
    // Queue is empty or not.
    isEmpty() {
        return (this.front == -1);
    }
     
    // Function to insert element at front
    // end of Output Restricted Queue
    insertfront(key)
    {
 
        // Check whether Deque if  full or not
        if (this.isFull()) {
            console.log("overflow");
            return;
        }
 
        // If queue is initially empty
        if (this.front == -1) {
            this.front = 0;
            this.rear = 0;
        }
 
        // Front is at first position of queue
        else if (this.front == 0)
            this.front = this.size - 1;
 
        else
 
            // Decrement front end by '1'
            this.front = this.front - 1;
 
        // Insert current element into Deque
        this.arr[this.front] = key;
    }
 
     
    // Function to insert element at rear end
    // of Output Restricted Queue
    insertrear(key)
    {
        // Check whether Deque if  full or not
        if (this.isFull()) {
            console.log("overflow");
            return;
        }
 
        // If queue is initially empty
        if (this.front == -1) {
            this.front = 0;
            this.rear = 0;
        }
 
        // Rear is at last position of queue
        else if (this.rear == this.size - 1)
            this.rear = 0;
 
        // Increment rear end by '1'
        else
            this.rear = this.rear + 1;
 
        // Insert current element into Deque
        this.arr[this.rear] = key;
    }
 
    // Deletes element at front end of
    // Output Restricted Queue
    deletefront()
    {
 
        // Check whether Deque
        // is empty or not
        if (this.isEmpty()) {
            console.log("Queue Underflow");
            return;
        }
 
        // Deque has only one element
        if (this.front == this.rear) {
            this.front = -1;
            this.rear = -1;
        }
        else
            // Back to initial position
            if (this.front == this.size - 1)
            this.front = 0;
 
        else
 
            // Increment front by '1' to remove
            // current front value from Deque
            this.front = this.front + 1;
    }
     
     
    // Returns front element of Output
    // Restricted Queue
    getFront()
    {
 
        // Check whether Deque
        // is empty or not
        if (this.isEmpty()) {
            console.log(" Underflow");
            return -1;
        }
        return this.arr[this.front];
    }
     
     
    // Function to return rear element of
    // Output Restricted Queue
    getRear()
    {
 
        // Check whether Deque
        // is empty or not
        if (this.isEmpty() || this.rear < 0) {
            console.log(" Underflow");
            return -1;
        }
        return this.arr[this.rear];
    }
 
};
 
// Driver code
let dq = new Deque(5);
 
// Function calls
document.write("Inserted element at rear end : 10 \n");
dq.insertrear(10);
 
document.write("Inserted element at rear end : 15 \n ");
dq.insertrear(15);
 
document.write("Inserted element at front end : 5 \n");
dq.insertfront(5);
 
document.write("Get rear element : ", dq.getRear());
document.write("\n");
document.write("Get front element : ", dq.getFront());
document.write("\n");
 
dq.deletefront();
 
document.write("After delete front element new front become : " , dq.getFront());
 
// The code is contributed by Nidhi goel.
 
</script>


C#




using System;
 
public class Deque
{
    private int[] items;
    private int front;
    private int rear;
    private int size;
    private int capacity;
 
    public Deque(int capacity)
    {
        this.capacity = capacity;
        items = new int[capacity];
        front = 0;
        rear = -1;
        size = 0;
    }
 
    public void insertrear(int item)
    {
        if (isFull())
            throw new OverflowException("Deque is full");
        rear = (rear + 1) % capacity;
        items[rear] = item;
        size++;
    }
 
    public void insertfront(int item)
    {
        if (isFull())
            throw new OverflowException("Deque is full");
        front = (front - 1 + capacity) % capacity;
        items[front] = item;
        size++;
    }
 
    public void deletefront()
    {
        if (isEmpty())
            throw new InvalidOperationException("Deque is empty");
        front = (front + 1) % capacity;
        size--;
    }
 
    public int getFront()
    {
        if (isEmpty())
            throw new InvalidOperationException("Deque is empty");
        return items[front];
    }
 
    public int getRear()
    {
        if (isEmpty())
            throw new InvalidOperationException("Deque is empty");
        return items[rear];
    }
 
    public bool isEmpty()
    {
        return size == 0;
    }
 
    public bool isFull()
    {
        return size == capacity;
    }
}
 
public class Program
{
    public static void Main()
    {
        Deque dq = new Deque(5);
 
        // Function calls
        Console.WriteLine("Inserted element at rear end : 10");
        dq.insertrear(10);
 
        Console.WriteLine("Inserted element at rear end : 15");
        dq.insertrear(15);
 
        Console.WriteLine("Inserted element at front end : 5");
        dq.insertfront(5);
 
        Console.WriteLine("Get rear element : {0}", dq.getRear());
 
        Console.WriteLine("Get front element : {0}", dq.getFront());
 
        dq.deletefront();
 
        Console.WriteLine("After delete front element new front become : {0}", dq.getFront());
    }
}


Output

Inserted element at rear end : 10 
Inserted element at rear end : 15 
Inserted element at front end : 5 
Get rear element :  15
Get front element : 5
After delete front element new front become : 10

Time Complexity: O(N)
Auxiliary Space: O(N)

Need to implement Output restricted queue:

  • It is needed when we have to inhibit deletion from the rear of the deque.
  • It is used in job scheduling algorithms.

Advantages of Output Restricted Queue:

  • Security of the system by restricting the delete method of the queue at the rear.

Disadvantages of Output Restricted Queue:

  • Can’t provide the added functionality in comparison to Deque.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads