0% found this document useful (0 votes)
3 views1 page

Queue Implementation 2

This document provides a C implementation of a queue data structure with functions for enqueueing, dequeueing, and displaying elements. It defines a maximum size for the queue and handles overflow and underflow conditions. The main function demonstrates the usage of these queue operations.

Uploaded by

rehan107968
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Queue Implementation 2

This document provides a C implementation of a queue data structure with functions for enqueueing, dequeueing, and displaying elements. It defines a maximum size for the queue and handles overflow and underflow conditions. The main function demonstrates the usage of these queue operations.

Uploaded by

rehan107968
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Queue Implementation in C

#include <stdio.h>
#define SIZE 5 // maximum size of queue

int queue[SIZE];
int front = -1, rear = -1;

// Function to insert (enqueue) an element


void enqueue(int value) {
if (rear == SIZE - 1) {
printf("Queue Overflow! Cannot insert %d\n", value);
} else {
if (front == -1) front = 0; // set front to 0 on first insertion
rear++;
queue[rear] = value;
printf("%d inserted into queue.\n", value);
}
}

// Function to remove (dequeue) an element


void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow! Cannot remove element.\n");
} else {
printf("%d removed from queue.\n", queue[front]);
front++;
}
}

// Function to display the queue


void display() {
if (front == -1 || front > rear) {
printf("Queue is empty.\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
enqueue(40);
enqueue(50);
enqueue(60); // will cause overflow
display();
return 0;
}

You might also like