KEMBAR78
Circular Queue | PDF | Queue (Abstract Data Type) | Integer (Computer Science)
0% found this document useful (0 votes)
62 views2 pages

Circular Queue

The document provides a C++ program to implement operations on a circular queue using arrays. The operations included are insertion, deletion and display. The program uses functions like insertCQ() to insert elements, deleteCQ() to delete elements and displayCQ() to display all the elements of the circular queue. It uses concepts like front and rear pointers, and modulo arithmetic to implement the circular nature of the queue.

Uploaded by

sachdevagngn13
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)
62 views2 pages

Circular Queue

The document provides a C++ program to implement operations on a circular queue using arrays. The operations included are insertion, deletion and display. The program uses functions like insertCQ() to insert elements, deleteCQ() to delete elements and displayCQ() to display all the elements of the circular queue. It uses concepts like front and rear pointers, and modulo arithmetic to implement the circular nature of the queue.

Uploaded by

sachdevagngn13
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
You are on page 1/ 2

Data Structures Lab Manual & Record MRCET

Week - 6
i)Write a program that implement Circular Queue (its operations) using Arrays .
ii) Write a program that uses both recursive and non recursive functions to
perform the following searching operations for a Key value in a given list of
integers:
a) Linear search b) Binary search

Aim: To write a C++ program to implement Circular queues operations using Arrays.

Programs:

i) Circular Queue:

#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1, n=5;

void insertCQ(int val) {


if ((front == 0 && rear == n-1) || (front == rear+1)) {
cout<<"Queue Overflow \n";
return;
}
if (front == -1) {
front = 0;
rear = 0;
} else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void deleteCQ() {
if (front == -1) {
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;

if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {

Department of Computer Science and Engineering 42


Data Structures Lab Manual & Record MRCET

cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<" ";
f++;
}
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {

int ch, val;


cout<<"1)Insert\n";
cout<<"2)Delete\n";
cout<<"3)Display\n";
cout<<"4)Exit\n";
do {
cout<<"Enter choice : "<<endl;
cin>>ch;
switch(ch) {
case 1:
cout<<"Input for insertion: "<<endl;
cin>>val;
insertCQ(val);
break;

case 2:
deleteCQ();
break;

case 3:
displayCQ();
break;

case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 4);
return 0;
}

Department of Computer Science and Engineering 43

You might also like