Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

linear queue simplified

The document contains a C program that implements a simple queue using an array. It includes functions to check if the queue is full or empty, add elements (enQueue), remove elements (deQueue), and display the current state of the queue. The main function demonstrates the usage of these queue operations with sample data.

Uploaded by

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

linear queue simplified

The document contains a C program that implements a simple queue using an array. It includes functions to check if the queue is full or empty, add elements (enQueue), remove elements (deQueue), and display the current state of the queue. The main function demonstrates the usage of these queue operations with sample data.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>
#define SIZE 5

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

// Check if the queue is full


int isFull() {
return rear == SIZE - 1;
}

// Check if the queue is empty


int isEmpty() {
return front == -1 || front > rear;
}

// Add an element to the queue


void enQueue(int element) {
if (isFull()) {
printf("\nQueue is full!!\n");
return;
}
if (front == -1) front = 0; // First element being added
items[++rear] = element;
printf("\nInserted -> %d", element);
}

// Remove an element from the queue


int deQueue() {
if (isEmpty()) {
printf("\nQueue is empty!!\n");
return -1;
}
int element = items[front++];
if (front > rear) { // Reset if the queue becomes empty
front = rear = -1;
}
printf("\nDeleted element -> %d\n", element);
return element;
}

// Display the queue


void display() {
if (isEmpty()) {
printf("\nEmpty Queue\n");
return;
}
printf("\nFront -> %d\nItems -> ", front);
for (int i = front; i <= rear; i++) {
printf("%d ", items[i]);
}
printf("\nRear -> %d\n", rear);
}

int main() {
enQueue(10);
enQueue(20);
enQueue(30);
display();

deQueue();
display();

enQueue(40);
enQueue(50);
enQueue(60); // Should indicate the queue is full
display();

return 0;
}

You might also like