In queue, insertion and deletion happen at the opposite ends, so implementation is not as simple as stack.
To implement a queue using array, create an array arr of size n and take two variables front and rear both of which will be initialized to 0 which means the queue is currently empty. Element rear is the index upto which the elements are stored in the array and front is the index of the first element of the array. Now, some of the implementation of queue operations are as follows:
- Enqueue: Addition of an element to the queue. Adding an element will be performed after checking whether the queue is full or not. If rear < n which indicates that the array is not full then store the element at arr[rear] and increment rear by 1 but if rear == n then it is said to be an Overflow condition as the array is full.
- Dequeue: Removal of an element from the queue. An element can only be deleted when there is at least an element to delete i.e. rear > 0. Now, element at arr[front] can be deleted but all the remaining elements have to shifted to the left by one position in order for the dequeue operation to delete the second element from the left on another dequeue operation.
- Front: Get the front element from the queue i.e. arr[front] if queue is not empty.
- Display: Print all element of the queue. If the queue is non-empty, traverse and print all the elements from index front to rear.
Below is the implementation of a queue using an array:
Queue is Empty 20 <-- 30 <-- 40 <-- 50 <-- Queue is full 20 <-- 30 <-- 40 <-- 50 <-- after two node deletion 40 <-- 50 <-- Front Element is: 40
Time Complexity of Enqueue : O(1)
Time Complexity of Dequeue : O(n)
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.
- Queue | Set 1 (Introduction and Array Implementation)
- Circular Queue | Set 1 (Introduction and Array Implementation)
- Queue - Linked List Implementation
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Implementation of Deque using circular array
- Binary Tree (Array implementation)
- Check if a queue can be sorted into another queue using a stack
- Stack and Queue in Python using queue Module
- Reversing a Queue using another Queue
- Dockerizing a simple Nodejs app
- Dockerizing a simple Django app
- A simple understanding of Computer
- Simple Chat Room using Python
- C/C++ program to make a simple calculator
- Downloading gists from Github made simple
- How to build a simple Calculator app using Android Studio?
- Creating a simple JSON based API using Node.js
- Compile our own Android Kernel in 5 Simple Steps
- Python | Program to implement simple FLAMES game
- LRU Cache Implementation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.