0% found this document useful (0 votes)
17 views4 pages

Linear Queue Operations in C

The document provides a C program that implements a linear queue using a linear array with a maximum size of 5. It includes functions for checking if the queue is full or empty, as well as operations for enqueueing, dequeueing, peeking at the front element, and displaying the queue. The program runs in a loop, allowing the user to perform various queue operations through a menu-driven interface.

Uploaded by

sheoransakshi72
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)
17 views4 pages

Linear Queue Operations in C

The document provides a C program that implements a linear queue using a linear array with a maximum size of 5. It includes functions for checking if the queue is full or empty, as well as operations for enqueueing, dequeueing, peeking at the front element, and displaying the queue. The program runs in a loop, allowing the user to perform various queue operations through a menu-driven interface.

Uploaded by

sheoransakshi72
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

Write a program to implement various operations on a linear queue represented using

a linear array.

#include <stdio.h>

#include <stdlib.h>

#define MAX 5 // Maximum size of queue

int queue[MAX];

int front = -1, rear = -1;

// Function to check if queue is full

int isFull() {

return rear == MAX - 1;

// Function to check if queue is empty

int isEmpty() {

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

// Enqueue operation

void enqueue(int value) {

if (isFull()) {

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

} else {

if (front == -1) front = 0; // First insertion

queue[++rear] = value;
printf("Inserted %d\n", value);

// Dequeue operation

void dequeue() {

if (isEmpty()) {

printf("Queue Underflow! Cannot delete.\n");

} else {

printf("Deleted %d\n", queue[front]);

front++;

// Peek operation (front element)

void peek() {

if (isEmpty()) {

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

} else {

printf("Front element: %d\n", queue[front]);

// Display operation

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");

// Main function

int main() {

int choice, value;

while (1) {

printf("\n--- Linear Queue Menu ---\n");

printf("1. Enqueue (Insert)\n");

printf("2. Dequeue (Delete)\n");

printf("3. Peek (Front)\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:
printf("Enter value to insert: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

peek();

break;

case 4:

display();

break;

case 5:

exit(0);

default:

printf("Invalid choice! Try again.\n");

return 0;

You might also like