Double Ended Queue

4

1 votes
Easy
Problem

A Double Ended Queue is a data structure in which it is possible to insert and delete from both ends (front and rear). Given an integer m, size of double ended queue, and n operations, implement the double ended queue. Queries are defined as below.

  • 1 x --> insert an element x to the front end
  • 2 x --> insert an element x to the rear end
  • 3 --> delete an element from front end
  • 4 --> delete an element from rear end


Input:
First line in the input is an integer m, size of the queue. Second line of the input is an integer n total number of queries. Followed by n queries of the above mentioned format.
Output:
If an insertion operation can not be performed print "Queue overflow".
If a deletion operation can not be performed print "Queue underflow".
Separate each output with a newline. In the end print the queue elements separated by spaces (from rear to front).


Constraints:
All the elements are well with in the integer range.

Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?