Enqueue Operation in Queue Data Structure
Enqueue() operation in Queue adds (or stores) an element to the end of the queue.
The following steps should be taken to enqueue (insert) data into a queue:
- Step 1: Check if the queue is full.
- Step 2: If the queue is full, return overflow error and exit.
- Step 3: If the queue is not full, increment the rear pointer to point to the next empty space.
- Step 4: Add the data element to the queue location, where the rear is pointing.
- Step 5: return success.
Implementation of Enqueue:
void queueEnqueue(int data)
{
// Check queue is full or not
if (capacity == rear) {
printf("\nQueue is full\n");
return;
}
// Insert element at the rear
else {
queue[rear] = data;
rear++;
}
return;
}
// Function to add an item to the queue.
// It changes rear and size
void enqueue(struct Queue* queue, int item)
{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queue\n", item);
}
// This code is contributed by Susobhan Akhuli
void queueEnqueue(int data)
{
// Check queue is full or not
if (capacity == rear) {
System.out.println("\nQueue is full\n");
return;
}
// Insert element at the rear
else {
queue[rear] = data;
rear++;
}
return;
}
// This code is contributed by aadityapburujwale
# Function to add an item to the queue.
# It changes rear and size
def EnQueue(self, item):
if self.isFull():
print("Full")
return
self.rear = (self.rear + 1) % (self.capacity)
self.Q[self.rear] = item
self.size = self.size + 1
print("% s enqueued to queue" % str(item))
# This code is contributed by Susobhan Akhuli
// Function to add an item to the queue.
// It changes rear and size
public void enqueue(int item)
{
if (rear == max - 1) {
Console.WriteLine("Queue Overflow");
return;
}
else {
ele[++rear] = item;
}
}
// This code is contributed by Susobhan Akhuli
<script>
enqueue(element){
// adding element to the queue
this.items.push(element);
}
// 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