0% found this document useful (0 votes)
16 views12 pages

P3 Dsa

Uploaded by

jaywareg003
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)
16 views12 pages

P3 Dsa

Uploaded by

jaywareg003
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/ 12

Name : Tejas Gitkhane

Roll no. : S511042


Class : SE

Div. :A

Batch : A2

DSA PRACTICAL-3
Implement a simple queue using array perform the following operation on it :
1)Insertion(Enqueue)
2)Deletion(Dequeue)
3)Display

Code:
#include <iostream>
using namespace std;
int queue[100], n = 100, front = -1, rear = -1;
void Insert() {
int val;
if (rear >= n - 1) {
cout << "Queue overflow" << endl;
} else {
if (front == -1)
front = 0;

cout << "Insert the Element in Queue: ";


cin >> val;
rear++;
queue[rear] = val;
}
}
void Delete() {
if (front == -1 || front > rear) {
cout << "Queue is underflow" << endl;
} else {
cout << "Element Deleted From queue is: " << queue[front] << endl;
front++;
if (front > rear) {
front = rear = -1;
}
}
}
void Display() {
if (front == -1) {
cout << "Queue is Empty" << endl;
} else {
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++)
cout << queue[i] << " ";
cout << endl;
}
}
int main() {
int a;
do {
cout << "1) Insert Element in queue" << endl;
cout << "2) Delete Element from queue" << endl;
cout << "3) Display Elements in the queue" << endl;
cout << "4) Exit" << endl;

cout << "Enter your Choice: ";


cin >> a;
switch (a) {
case 1:
Insert();
break;
case 2:
Delete();
break;
case 3:
Display();
break;
case 4:
cout << "Exit" << endl;
break;
default:
cout << "Invalid Choice" << endl;
}
} while (a != 4);

return 0;
}
Output :
Implement a circular queue using array perform the following operation
on it :
1)Insertion(Enqueue)
2)Deletion(Dequeue)
3)Display

Code:
#include <iostream>

using namespace std;

int cirqueue[10];

int front = -1, rear = -1;

int size1 = 10;

void enqueue(int value) {

if ((front == 0 && rear == size1 - 1) || (front == rear + 1)) {

cout << "Queue Overflow" << endl;

return;

if (front == -1) {

front = 0;

rear = 0;

} else {

if (rear == size1 - 1)

rear = 0;

else

rear++;

cirqueue[rear] = value;

}
void dequeue() {

if (front == -1) {

cout << "Queue Underflow" << endl;

return;

cout << "Element deleted from queue is: " << cirqueue[front] << endl;

if (front == rear) {

front = -1;

rear = -1;

} else {

if (front == size1 - 1)

front = 0;

else

front++;

void display() {

if (front == -1) {

cout << "Queue is empty" << endl;

return;

cout << "Queue elements are: " << endl;

int f1 = front, r1 = rear;

if (f1 <= r1) {

while (f1 <= r1) {

cout << cirqueue[f1] << " ";

f1++;

}
} else {

while (f1 <= size1 - 1) {

cout << cirqueue[f1] << " ";

f1++;

f1 = 0;

while (f1 <= r1) {

cout << cirqueue[f1] << " ";

f1++;

cout << endl;

int main() {

int x, value;

cout << "Press 1 to Enqueue" << endl;

cout << "Press 2 to Dequeue" << endl;

cout << "Press 3 to Display" << endl;

cout << "Press 4 to Exit" << endl;

do {

cout << "Enter your choice: " ;

cin >> x;

switch (x) {

case 1:

cout << "Input for insertion: " ;

cin >> value;


cout<<"-------------------------------------------------------------------"<<endl;

enqueue(value);

break;

case 2:

dequeue();

cout<<"-------------------------------------------------------------------"<<endl;

break;

case 3:

display();

cout<<"-------------------------------------------------------------------"<<endl;

break;

case 4:

cout << "Exit" << endl;

break;

default:

cout << "Incorrect choice!" << endl;

} while (x != 4);

return 0;

}
OUTPUT :

You might also like