0% found this document useful (0 votes)
5 views3 pages

Queue ADT using Array

The document contains a C program that implements a simple queue using an array with a maximum size of 5. It includes functions for enqueueing, dequeueing, and displaying queue elements, along with a main function to test these operations. The program demonstrates handling of full and empty queue conditions.

Uploaded by

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

Queue ADT using Array

The document contains a C program that implements a simple queue using an array with a maximum size of 5. It includes functions for enqueueing, dequeueing, and displaying queue elements, along with a main function to test these operations. The program demonstrates handling of full and empty queue conditions.

Uploaded by

chandrak1302
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>

#define MAX 5 // Maximum queue size

int Queue[MAX]; // Array to store queue elements

int Front = -1, Rear = -1; // Initialize front and rear

// Function to insert an element in the queue (Enqueue)

void Enqueue(int value)

if (Rear == MAX - 1 ) // To check if the queue is full

printf("Queue is Full! Cannot Enqueue %d\n", value);

return;

else if (Front == -1 && Rear == -1)

Front = Rear = 0; // Set front to 0 if queue was empty

Queue[Rear] = value; // Insert value at rear

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

else

Rear ++ ;

Queue[Rear] = value; // Insert value at rear

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

// Function to remove an element from the queue (Dequeue)

int Dequeue()

if (Rear == -1 && Front == -1)

{
printf("Queue is Empty! Cannot Dequeue\n");

return -1;

int value = Queue[Front];

if (Front == Rear)

// If only one element was present

Front = Rear = -1;

else

Front++;

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

return value;

// Function to display queue elements

void Display()

if (Front == -1 && Rear == -1)

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

return;

printf("Queue elements: ");

for (int i = Front; i <= Rear; i++) {

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

printf("\n");

// Main function to test the queue


int main()

Enqueue(10);

Enqueue(20);

Enqueue(30);

Display(); // 10 20 30

Dequeue();

Display(); // 20 30

Enqueue(40);

Enqueue(50);

Enqueue(60); // Queue is full

Display(); // 20 30 40 50

return 0;

You might also like