What is Queue?
is a linear data structure that follows FIFO approach (First In First Out). One can imagine a queue as a line of people waiting in sequential order which starts from the beginning of the line. It is an ordered list in which insertions are done at one end which is known as the rear and deletions are done from the other end known as the front. A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. A queue can be implemented using Arrays or Linked Lists.
Complexity analysis of different Queue operations:
1) enqueue():
This operation inserts an element at the back of the queue. It takes one parameter, the value that is to be inserted at the back of the queue.
Below is the implementation of enqueue() using Array:
Output1 inserted successfully
2 inserted successfully
Complexity Analysis:
- Time Complexity: O(1), In enqueue function a single element is inserted at the last position. This takes a single memory allocation operation which is done in constant time.
- Auxiliary Space: O(1). As no extra space is being used.
Below is the implementation of enqueue() using Linked List :
Output1 inserted successfully
2 inserted successfully
Complexity Analysis:
- Time Complexity: O(1). Only a new node is created and the pointer of the last node is updated. This includes only memory allocation operations. Hence it can be said that insertion is done in constant time.
- Auxiliary Space: O(1). No extra space is used.
2) dequeue():
This operation removes an element present at the front of the queue. Also, it results in an error if the queue is empty.
Below is the implementation of dequeue() using Array :
OutputElement deleted from queue : 1
Complexity Analysis:
- Time Complexity: O(1). In array implementation, only an arithmetic operation is performed i.e., the front pointer is incremented by 1. This is a constant time function.
- Auxiliary Space: O(1). No extra space is utilized for deleting an element from the queue.
Below is the implementation of dequeue using Linked List :
OutputElement deleted from queue is : 5
Complexity Analysis:
- Time Complexity: O(1). In dequeue operation, only the first node is deleted and the front pointer is updated. This is a constant time operation.
- Auxiliary Space: O(1). No extra space is utilized for deleting an element from the queue.
3) peek():
This operation prints the element present at the front of the queue.
Below is the implementation of peek() using Array:
OutputElement at the front of queue: 1
Complexity Analysis:
- Time Complexity: O(1). In this operation, only a memory address is accessed. This is a constant time operation.
- Auxiliary Space: O(1). No extra space is utilized to access the first value.
Below is the implementation of peek() using Linked List:
OutputElement present at the front of queue: 5
Complexity Analysis:
- Time Complexity: O(1). In linked list implementation also a single memory address is accessed. It takes constant time.
- Auxiliary Space: O(1). No extra space is utilized to access the first element.
4) initialize():
This operation takes an array and adds the element at the back of the Queue.
Implementation of initialize() using array:
Complexity Analysis:
- Time Complexity: O(N). Inserting each element is a constant time operation. So for inserting N elements N unit of time is required.
- Auxiliary Space: O(N). N elements are inserted.
Implementation of initialize() using LinkedList:
Complexity Analysis:
- Time Complexity: O(N). Creating a new node and making a link takes unit time. So to insert N elements (i.e., creating N nodes and linking them) N unit of times is required.
- Auxiliary Space: O(N). N elements need to be inserted.
5) isfull():
Function that returns true if the queue is filled completely else returns false.
Below is the implementation of isfull() using array:
OutputQueue is not filled completely
Complexity Analysis:
- Time Complexity: O(1). It only performs an arithmetic operation to check if the queue is full or not.
- Auxiliary Space: O(1). It requires no extra space.
Below is the implementation of isfull() using Linked List:
OutputQueue is not filled completely
Complexity Analysis:
- Time Complexity: O(N). The whole linked list is traversed to calculate the length and then the length is checked with the capacity. The traversal of the linked list takes O(N) time.
- Auxiliary Space: O(1). No extra space is required.
6) isempty():
Function that returns true if the queue is empty else returns false.
Below is the implementation of isempty() operation using array:
Complexity Analysis:
- Time Complexity: O(1) It only checks the position stored in the first and last pointer
- Auxiliary Space: O(1) NO extra space is required to check the values of the first and the last pointer.
Below is the implementation of isempty() operation using LinkedList:
Complexity Analysis:
- Time Complexity: O(1), It checks if the pointer of first is Null or not. This operation takes constant time.
- Auxiliary Space: O(1). No extra space is required.