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

Queue Operations in C and C++

The document contains C and C++ programs that implement linear and circular queues with functionalities for insertion, deletion, and traversal. It includes error handling for queue overflow and underflow conditions. The C++ program also features a menu-driven interface for user interaction.

Uploaded by

nityatrivedi.cps
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)
17 views9 pages

Queue Operations in C and C++

The document contains C and C++ programs that implement linear and circular queues with functionalities for insertion, deletion, and traversal. It includes error handling for queue overflow and underflow conditions. The C++ program also features a menu-driven interface for user interaction.

Uploaded by

nityatrivedi.cps
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

C PROGRAM TO IMPLEMENT INSERT, DELETE, TRAVERSAL IN LINEAR QUEUE

#include<stdio.h>

#define MAXSIZE 5

void initialize();

void lqinsert();

void lqdelete();

void lqtraverse();

int queue[MAXSIZE];

int front,rear;

void main()

initialize();

lqinsert();

lqinsert();

lqinsert();

lqinsert();

lqdelete();

lqtraverse();

void initialize()

front=rear=-1;

void lqinsert()

int num;

if(rear==MAXSIZE-1)

printf("\nQueue is full (Queue overflow)");

return;

}
printf("\nEnter the element to be inserted : ");

scanf("%d",&num);

rear++;

queue[rear]=num;

if(front==-1)

front=0;

void lqdelete()

if(front==-1)

printf("\nQueue is empty (Queue underflow)");

return;

int num;

num=queue[front];

printf("\nDeleted element is : %d",num);

front++;

if(front>rear)

front=rear=-1;

} void lqtraverse()

if(front==-1)

printf("\nQueue is empty (Queue underflow)");

return;

else

printf("\nQueue elements are : \n");

for(int i=front;i<=rear;i++)
printf("%d\t",queue[i]);

}
C PROGRAM TO IMPLEMENT INSERT, DELETE AND TRAVERSAL ON A
CIRCULAR QUEUE
#include <stdio.h>

#define MAXSIZE 5

int queue[MAXSIZE];

int front = -1, rear = -1;

void initialize() {

front = rear = -1;

void lqinsert(int num) {

if ((rear + 1) % MAXSIZE == front) {

printf("\nQueue is full (Queue overflow)\n");

return;

if (front == -1) {

front = 0;

rear = (rear + 1) % MAXSIZE;

queue[rear] = num;

printf("\nInserted %d into queue\n", num);

void lqdelete() {

if (front == -1) {

printf("\nQueue is empty (Queue underflow)\n");

return;

int num = queue[front];

if (front == rear) {

front = rear = -1;

} else {

front = (front + 1) % MAXSIZE;


}

printf("\nDeleted element is: %d\n", num);

void lqtraverse() {

if (front == -1) {

printf("\nQueue is empty (Queue underflow)\n");

return;

printf("\nQueue elements are:\n");

int i = front;

while (i != rear) {

printf("%d\t", queue[i]);

i = (i + 1) % MAXSIZE;

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

int main() {

initialize();

lqinsert(10);

lqinsert(20);

lqinsert(30);

lqinsert(40);

lqinsert(50);

lqinsert(60); // This should trigger queue overflow

lqdelete();

lqdelete();

lqtraverse();

return 0;

}
C++ PROGRAM TO IMPLEMENT LINEAR QUEUES
#include <iostream>

#include <cstdlib>

using namespace std;

const int MAXSIZE = 5;

int queueArr[MAXSIZE];

int front = 0, rear = 0;

bool isEmpty() {

return (front == rear);

bool isFull() {

return (rear == MAXSIZE);

void insertItem(int item) {

if (isFull()) {

cout << "Queue is full (overflow)\n";

} else {

queueArr[rear++] = item;

cout << "Inserted " << item << endl;

void deleteItem() {

if (isEmpty()) {

cout << "Queue is empty (underflow)\n";

} else {

int item = queueArr[front++];


cout << "Deleted element is: " << item << endl;

void displayQueue() {

if (isEmpty()) {

cout << "Queue is empty\n";

} else {

cout << "Queue elements are:\n";

for (int i = front; i < rear; i++)

cout << queueArr[i] << "\t";

cout << endl;

int main() {

int choice, val;

do {

cout << "\nMenu:\[Link] [Link] [Link] [Link]\nEnter choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter value to insert: ";

cin >> val;

insertItem(val);

break;

case 2:

deleteItem();

break;
case 3:

displayQueue();

break;

case 4:

cout << "Exiting...\n";

break;

default:

cout << "Invalid choice\n";

} while (choice != 4);

return 0;

You might also like