0% found this document useful (0 votes)
4 views

Queue implementation using array

This document presents a C program that implements a queue using an array with a maximum size of 5. It includes functions for enqueueing, dequeueing, peeking at the front element, and displaying the queue contents. The main function demonstrates the usage of these operations by enqueuing three elements, dequeuing one, and displaying the queue at different stages.

Uploaded by

chandrak1302
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Queue implementation using array

This document presents a C program that implements a queue using an array with a maximum size of 5. It includes functions for enqueueing, dequeueing, peeking at the front element, and displaying the queue contents. The main function demonstrates the usage of these operations by enqueuing three elements, dequeuing one, and displaying the queue at different stages.

Uploaded by

chandrak1302
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Queue implementation using array

#include <stdio.h>

#define MAX 5 // Maximum queue size

int queue[MAX]; // Array for queue

int front = -1, rear = -1; // Front and rear pointers

// Function prototypes

int isFull();

int isEmpty();

void enqueue(int value);

int dequeue();

int peek();

void display();

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

display();

printf("Dequeued: %d\n", dequeue());

printf("Front element: %d\n", peek());

display();

return 0;

// Check if queue is full

int isFull() {

return rear == MAX - 1;


}

// Check if queue is empty

int isEmpty() {

return front == -1 || front > rear;

// Enqueue operation (insert at rear)

void enqueue(int value) {

if (isFull()) {

printf("Queue Overflow! Cannot enqueue %d\n", value);

return;

if (isEmpty()) {

front = 0; // Reset front if inserting first element

rear=rear+1;

queue[rear] = value;

printf("Enqueued %d\n", value);

// Dequeue operation (remove from front)

int dequeue() {

if (isEmpty()) {

printf("Queue Underflow! Cannot dequeue\n");

return -1;

front=front+1;

int value = queue[front];

if (front > rear) { // Reset queue if empty

front = rear = -1;

}
return value;

// Peek operation (return front element)

int peek() {

if (isEmpty()) {

printf("Queue is empty!\n");

return -1;

return queue[front];

// Display queue elements

void display() {

if (isEmpty()) {

printf("Queue is empty!\n");

return;

printf("Queue elements: ");

for (int i = front; i <= rear; i++)

printf("%d ", queue[i]);

printf("\n");

You might also like