0% found this document useful (0 votes)
4 views6 pages

Linear Queue Implementation in C

The document provides a detailed implementation of a linear queue using arrays in C, including functions for enqueueing, dequeueing, checking if the queue is full or empty, and displaying the queue elements. It demonstrates the operations with example values and handles both overflow and underflow conditions. The code is structured with clear function definitions and includes a main function to execute the queue operations.

Uploaded by

milangaikwad0960
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)
4 views6 pages

Linear Queue Implementation in C

The document provides a detailed implementation of a linear queue using arrays in C, including functions for enqueueing, dequeueing, checking if the queue is full or empty, and displaying the queue elements. It demonstrates the operations with example values and handles both overflow and underflow conditions. The code is structured with clear function definitions and includes a main function to execute the queue operations.

Uploaded by

milangaikwad0960
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

Practical No.

21

 Linear Queue (Array):

int q[n],f=-1,r=-1,val; //De_Queue( ) [Aray]:


void D_Queue()
//En_Queue( ) [Array]: {
void En_Queue() if(f==-1 && r==-1)
{ {
if(r==n-1) printf("Queue is Underflow.\n");
{ return ;
printf("Queue Overflow.\n"); }
return ; else
} {
else val=q[f];
{ printf("%d is Removed.\n",val);
printf("Enter Element to Insert: "); if(f==r)
scanf("%d",&val); {
if(f==-1 && r==-1) f=r=-1;
{ }
f=r=0; else
q[r]=val; {
printf("Enqueue %d in Queue.",val); f=f+1;
} }
else }
{ }
r=r+1;
q[r]=val;
printf("Enqueue %d in Queue.",val);
}
}
}
#include <stdio.h>

#define SIZE 5

Int queue[SIZE];

Int front = -1, rear = -1;

// Function to check if queue is full

Int isFull() {

Return rear == SIZE – 1;

// Function to check if queue is empty

Int isEmpty() {

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

// Function to enqueue (insert) an element

Void enqueue(int value) {

If (isFull()) {

Printf(“Queue is FULL! Cannot insert %d\n”, value);

} else {

If (front == -1) front = 0; // First element insertion

Queue[++rear] = value;

Printf(“Enqueued: %d\n”, value);

// Function to dequeue (remove) an element

Void dequeue() {
If (isEmpty()) {

Printf(“Queue is EMPTY! Cannot dequeue\n”);

} else {

Printf(“Dequeued: %d\n”, queue[front++]);

// Function to display current queue elements

Void display() {

If (isEmpty()) {

Printf(“Queue is EMPTY!\n”);

} else {

Printf(“Current Queue: “);

For (int I = front; I <= rear; i++) {

Printf(“%d “, queue[i]);

Printf(“\n”);

Int main() {

// Performing given operations

Enqueue(15);

Enqueue(48);

Enqueue(69);

Display();

Dequeue();

Display();
Enqueue(12);

Enqueue(23);

Display();

Dequeue();

Dequeue();

Display();

// Checking queue status

If (isEmpty())

Printf(“Queue is now EMPTY.\n”);

Else if (isFull())

Printf(“Queue is FULL.\n”);

Else

Printf(“Queue is neither full nor empty.\n”);

Return 0;

#include <stdio.h>

#define SIZE 3 // Small size for easy demonstration

Int queue[SIZE];

Int front = -1, rear = -1;

// Function to check if queue is full

Int isFull() {

Return rear == SIZE – 1;

}
// Function to check if queue is empty

Int isEmpty() {

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

// Function to enqueue (insert)

Void enqueue(int value) {

If (isFull()) {

Printf(“Queue Overflow! Cannot insert %d\n”, value);

} else {

If (front == -1) front = 0;

Queue[++rear] = value;

Printf(“Enqueued: %d\n”, value);

// Function to dequeue (remove)

Void dequeue() {

If (isEmpty()) {

Printf(“Queue Underflow! No elements to delete.\n”);

} else {

Printf(“Dequeued: %d\n”, queue[front++]);

// Function to display current queue

Void display() {

If (isEmpty()) {
Printf(“Queue is EMPTY!\n”);

} else {

Printf(“Queue elements: “);

For (int I = front; I <= rear; i++) {

Printf(“%d “, queue[i]);

Printf(“\n”);

Int main() {

Printf(“Demonstrating Queue Overflow and Underflow:\n\n”);

// Demonstrate overflow

Enqueue(10);

Enqueue(20);

Enqueue(30);

Enqueue(40); // Overflow should occur here

Display();

// Demonstrate underflow

Dequeue();

Dequeue();

Dequeue();

Dequeue(); // Underflow should occur here

Display();

Return 0;

You might also like