Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends.In previous post we had discussed introduction of deque. Now in this post we see how we implement deque Using circular array.
Operations on Deque:
Mainly the following four basic operations are performed on queue:
insetFront(): Adds an item at the front of Deque.
insertRear(): Adds an item at the rear of Deque.
deleteFront(): Deletes an item from front of Deque.
deleteRear(): Deletes an item from rear of Deque.
In addition to above operations, following operations are also supported
getFront(): Gets the front item from queue.
getRear(): Gets the last item from queue.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.
Circular array implementation deque
For implementing deque, we need to keep track of two indices, front and rear. We enqueue(push) an item at the rear or the front end of qedue and dequeue(pop) an item from both rear and front end.
1. Create an empty array ‘arr’ of size ‘n’
initialize front = -1 , rear = 0
Inserting First element in deque, at either front or rear will lead to the same result.
After insert Front Points = 0 and Rear points = 0
Insert Elements at Rear end
a). First we check deque if Full or Not b). IF Rear == Size-1 then reinitialize Rear = 0 ; Else increment Rear by '1' and push current key into Arr[ rear ] = key Front remain same.
Insert Elements at Front end
a). First we check deque if Full or Not b). IF Front == 0 || initial position, move Front to points last index of array front = size - 1 Else decremented front by '1' and push current key into Arr[ Front] = key Rear remain same.
Delete Element From Rear end
a). first Check deque is Empty or Not b). If deque has only one element front = -1 ; rear =-1 ; Else IF Rear points to the first index of array it's means we have to move rear to points last index [ now first inserted element at front end become rear end ] rear = size-1 ; Else || decrease rear by '1' rear = rear-1;
Delete Element From Front end
a). first Check deque is Empty or Not b). If deque has only one element front = -1 ; rear =-1 ; Else IF front points to the last index of the array it's means we have no more elements in array so we move front to points first index of array front = 0 ; Else || increment Front by '1' front = front+1;
Below is the implementation of above idea.
insert element at rear end : 5 insert element at rear end : 10 get rear element : 10 After delete rear element new rear become : 5 inserting element at front end get front element : 15 After delete front element new front become : 5
Time Complexity: Time complexity of all operations like insertfront(), insertlast(), deletefront(), deletelast()is O(1).
In mext post we will discuss deque implementation using Doubly linked list.
This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Circular Queue | Set 1 (Introduction and Array Implementation)
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Implementation of Deque using doubly linked list
- deque::front() and deque::back() in C++ STL
- deque::clear() and deque::erase() in C++ STL
- Circular array
- Queue | Set 1 (Introduction and Array Implementation)
- Array implementation of queue (Simple)
- Majority element in a circular array of 0's and 1's
- deque::at() and deque::swap() in C++ STL
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Find the next greater element in a Circular Array
- Maximize sum of consecutive differences in a circular array
- Minimum bit changes in Binary Circular array to reach a index
- Convert an Array to a Circular Doubly Linked List
- Minimum absolute difference of adjacent elements in a circular array
- Minimum number of colors required to color a Circular Array
- Minimize the maximum absolute difference of adjacent elements in a circular array
- deque::operator= and deque::operator in C++ STL
- Deque in Python
Improved By : programmer2k17