Dequeue Operation in Queue Data Structure
Removes (or access) the first element from the queue.
The following steps are taken to perform the dequeue operation:
- Step 1: Check if the queue is empty.
- Step 2: If the queue is empty, return the underflow error and exit.
- Step 3: If the queue is not empty, access the data where the front is pointing.
- Step 4: Increment the front pointer to point to the next available data element.
- Step 5: The Return success.
Implementation of dequeue:
void queueDequeue()
{
// If queue is empty
if (front == rear) {
printf("\nQueue is empty\n");
return;
}
// Shift all the elements from index 2
// till rear to the left by one
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// decrement rear
rear--;
}
return;
}
// Function to remove an item from queue.
// It changes front and size
int dequeue(struct Queue* queue)
{
if (isEmpty(queue)) {
printf("\nQueue is empty\n");
return;
}
int item = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size = queue->size - 1;
return item;
}
// This code is contributed by Susobhan Akhuli
void queueDequeue()
{
// If queue is empty
if (front == rear) {
System.out.println("\nQueue is empty\n");
return;
}
// Shift all the elements from index 2
// till rear to the left by one
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// decrement rear
rear--;
}
return;
}
// This code is contributed by aadityapburujwale
# Function to remove an item from queue.
# It changes front and size
def DeQueue(self):
if self.isEmpty():
print("Queue is empty")
return
print("% s dequeued from queue" % str(self.Q[self.front]))
self.front = (self.front + 1) % (self.capacity)
self.size = self.size - 1
# This code is contributed by Susobhan Akhuli
// Function to remove an item from queue.
// It changes front and size
public int dequeue()
{
if (front == rear + 1) {
Console.WriteLine("Queue is Empty");
return -1;
}
else {
int p = ele[front++];
return p;
}
}
// This code is contributed by Susobhan Akhuli
<script>
dequeue(){
// removing element from the queue
// returns underflow when called
// on empty queue
if(this.isEmpty()){
document.write("<br>Queue is empty<br>");
return -1;
}
return this.items.shift();
}
// This code is contributed by Susobhan Akhuli
</script>
Introduction to Queue Data Structure
Queue Data Structure is a linear data structure that follows FIFO (First In First Out) Principle, so the first element inserted is the first to be popped out. In this article, we will cover all the basics of Queue, Operations on Queue, its implementation, advantages, disadvantages which will help you solve all the problems based on Queue.
Table of Content
- What is Queue Data Structure?
- Representation of Queue Data Structure:
- Types of Queue Data Structure
- Basic Operations in Queue Data Structure
- 1. Enqueue Operation in Queue Data Structure
- 2. Dequeue Operation in Queue Data Structure
- 3. Front Operation in Queue Data Structure
- 4. Rear Operation in Queue Data Structure
- 5. isEmpty Operation in Queue Data Structure
- 6. isFull Operation in Queue Structure
- Implementation of Queue Data Structure
- Complexity Analysis of Operations on Queue Data Structure
- Applications of Queue Data Structure
- Advantages of Queue Data Structure
- Disadvantages of Queue Data Structure
- FAQs (Frequently asked questions) on Queue Data Structure
Contact Us