Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Linear Queue PGM

Uploaded by

GOWRI KANNAN
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)
2 views6 pages

Linear Queue PGM

Uploaded by

GOWRI KANNAN
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/ 6

C Program to Implement a Menu-Driven Linear Queue using Array

#include <stdio.h>
#include <stdlib.h>

int front, rear, size;

// Function to initialize queue


void InitializeQueue() {
front = -1;
rear = -1;
}

// Function to check if queue is full


int IsQueueFull() {
return (rear == size - 1);
}

// Function to check if queue is empty


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

// Function to insert an item


void Insert(int Queue[], int item) {
if (IsQueueFull()) {
printf("Queue is Full. Cannot insert %d\n", item);
} else {
if (front == -1) // First element insertion
front = 0;
rear++;
Queue[rear] = item;
printf("%d inserted into queue.\n", item);
}
}

// Function to delete an item


void Delete(int Queue[]) {
if (IsQueueEmpty()) {
printf("Queue is Empty. Cannot delete.\n");
} else {
printf("%d deleted from queue.\n", Queue[front]);
front++;
}
}

// Function to get rear element


int RearElement(int Queue[]) {
if (IsQueueEmpty()) {
printf("Queue is Empty.\n");
return -1;
} else {
return Queue[rear];
}
}

// Function to get front element


int FrontElement(int Queue[]) {
if (IsQueueEmpty()) {
printf("Queue is Empty.\n");
return -1;
} else {
return Queue[front];
}
}

// Function to display queue


void Display(int Queue[]) {
if (IsQueueEmpty()) {
printf("Queue is Empty.\n");
} else {
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", Queue[i]);
if (i == front) printf("(Front)"); // Mark front
if (i == rear) printf("(Rear)"); // Mark rear
printf(" <- ");
}
printf("NULL\n");
}
}

// Main function to test queue operations


int main() {
printf("Enter the size of the queue: ");
scanf("%d", &size);

int Queue[size];
InitializeQueue();

int choice, item;

while (1) {
printf("\n--- Queue Menu ---\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Front Element\n");
printf("4. Rear Element\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter element to insert: ");
scanf("%d", &item);
Insert(Queue, item);
break;
case 2:
Delete(Queue);
break;
case 3:
item = FrontElement(Queue);
if (item != -1) printf("Front element: %d\n", item);
break;
case 4:
item = RearElement(Queue);
if (item != -1) printf("Rear element: %d\n", item);
break;
case 5:
Display(Queue);
break;
case 6:
exit(0);
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}

You might also like