0% found this document useful (0 votes)
31 views5 pages

Assign 3 DSA

This document contains C++ code that implements a circular queue using an array. It defines functions for insertion (enqueue), deletion (dequeue), and displaying the queue contents forward and reverse. The main function runs a do-while loop that allows the user to choose between these queue operations and exit. It demonstrates inserting and removing multiple elements from the circular queue.

Uploaded by

Utkal Pansare
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)
31 views5 pages

Assign 3 DSA

This document contains C++ code that implements a circular queue using an array. It defines functions for insertion (enqueue), deletion (dequeue), and displaying the queue contents forward and reverse. The main function runs a do-while loop that allows the user to choose between these queue operations and exit. It demonstrates inserting and removing multiple elements from the circular queue.

Uploaded by

Utkal Pansare
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/ 5

ASSIGNMENT 3

Roll No : 2041
Name : Vaishnavi Appasaheb Nemane
Tittle : Implement Circular Queue using Array. Perform following operations on it. a) Insertion
(Enqueue) b) Deletion (Dequeue) c) Display.

#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_forward() {
int f = front, r = rear;
if (front == -1) {
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;
}

void displayCQ_reverse() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[r]<<" ";
r--;
}
} else {

while (r>=0) {
cout<<cqueue[r]<<" ";
r--;
}
r=n-1;
while (r>=f) {
cout<<cqueue[r]<<" ";
r--;
}
}
cout<<endl;
}
int main() {

int ch, val;


cout<<"1)Insert\n";
cout<<"2)Delete\n";
cout<<"3)Display forward\n";
cout<<"4)Display reverse\n";
cout<<"5)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_forward();
break;
case 4:
displayCQ_reverse();
break;
case 5:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 5);
return 0;
}

OUTPUT:
[adamin@fedora Documents]$ cd Documents
[adamin@fedora Documents]$ g++ assign3.cpp
[adamin@fedora Documents]$ ./a.out
1.Insert
2.Delete
3.Display Forward
4.Display Reverse
5.Exit
Enter your choice :
1
Input for Insertion :
23
Enter your choice :
1
Input for Insertion :
67
Enter your choice :
1
Input for Insertion :
74
Enter your choice :
1
Input for Insertion :
90
Enter your choice :
3
Queue elements are:
23 67 74 90
Enter your choice :
4
Queue elements are:
90 74 67 23
Enter your choice :
2
elements deleted from queue is : 23
Enter your choice :
3
Queue elements are:
67 74 90
Enter your choice :
2
elements deleted from queue is : 67
Enter your choice :
3
Queue elements are:
74 90
Enter your choice :
2
elements deleted from queue is : 74
Enter your choice :
2
elements deleted from queue is : 90
Enter your choice :
2
Queue underflow
Enter your choice :
5
Exit[adamin@fedora Documents]$

You might also like