Name 2
Name 2
Name 2
CMS= 294-2023
Program= AD-CIS
Course= DSA
Lab 7
Q1
#include <iostream>
using namespace std;
int queue[50], n = 50, front = -1, rear = -1;
void insert() {
int num;
if (rear == n - 1)
cout << "queue Overflow" << endl;
else {
if (front == -1)
front = 0;
cout << "Insert element in queue " << endl;
cin >> num;
rear++;
queue[rear] = num;
}
}
void remove() {
if (front == -1 || front > rear) {
cout << "queue Underflow ";
return;
}
else {
cout << "Element removed from queue is : " << queue[front] << endl;
front++;;
}
}
void display() {
if (front == -1)
cout << "Queue is empty" << endl;
else {
cout << "Queue elements ";
for (int i = front; i <= rear; i++)
cout << queue[i] << " ";
cout << endl;
}
}
int main() {
int choice;
cout << "1. insert element to queue" << endl;
cout << "2. delete element from queue" << endl;
cout << "3. Show elements" << endl;
cout << "4. Exit" << endl;
do {
cout << "Enter your choice : " << endl;
cin >> choice;
switch (choice) {
case 1:
insert();
break;
case 2:
remove();
break;
case 3:
display();
break;
case 4:
cout << "Exit" << endl;
break;
default: cout << "Invalid choice" << endl;
}
} while (choice != 4);
return 0;
}
Q2
#include <iostream>
using namespace std;
int queue[50], n = 6, front = -1, rear = -1;
void insert(int x) {
if (rear == n - 1)
cout << "Queue Overflow" << endl;
else {
if (front == -1)
front = 0;
rear++;
queue[rear] = x;
}
}
void remove() {
if (front == -1 || front > rear) {
cout << "Queue Underflow ";
return;
}
else {
cout << "removed " << queue[front] << endl;
front++;;
}
}
void Show() {
if (front == -1)
cout << "Queue is empty " << endl;
else {
cout << "elements in queue ";
for (int i = front; i <= rear; i++)
cout << queue[i] << " ";
cout << endl;
}
}
int main() {
insert(5);
insert(2);
insert(3);
remove();
remove();
insert(6);
insert(3);
Show();
return 0;
}
Q3
#include <iostream>
using namespace std;
int queue[5];
int front = -1, rear = -1, n=50;
void insert(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;
}
queue[rear] = val ;
}
void remove() {
if (front == -1) {
cout<<"Queue Underflow"<<endl;
return ;
}
cout<<"Element deleted are : "<<queue[front]<<endl;
if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void show() {
int f = front, r = rear;
if (front == -1) {
cout<<"queue is empty"<<endl;
return;
}
cout<<"Queue elements are: ";
if (front <= rear) {
while (front <= rear){
cout<<queue[front]<<" ";
front++;
}
} else {
while (front <= n - 1) {
cout<<queue[front]<<" ";
front++;
}
front = 0;
while (front <= rear) {
cout<<queue[front]<<" ";
front++;
}
}
cout<<endl;
}
int main() {
int choice, val;
cout<<"1 Insert\n";
cout<<"2 Delete\n";
cout<<"3 Display\n";
cout<<"4 Exit\n";
do {
cout<<"Enter choice : "<<endl;
cin>>choice;
switch(choice) {
case 1:
cout<<"Enter elements to insert: ";
cin>>val;
insert(val);
break;
case 2:
remove();
break;
case 3:
show();
break;
case 4:
cout<<"Exit\n";
break;
default: cout<<"wrong input"<<endl;
}
} while(choice != 4);
return 0;
}
Q4
#include <iostream>
using namespace std;
int queue[100];
int front = -1, rear = -1, n = 6;
void insert(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++;
}
queue[rear] = val;
}
void remove() {
if (front == -1) {
cout << "Queue Underflow\n";
return;
}
cout << "Element Deleted is: " << queue[front] << endl;
if (front == rear) {
front = -1;
rear = -1;
}
else {
if (front == n - 1)
front = 0;
else
front++;
}
}
void show() {
int f = front, r = rear;
if (front == -1) {
cout << "Queue is empty" << endl;
return;
}
cout << "Queue elements are :";
if (front <= rear) {
while (front <= rear){
cout << queue[front] << " ";
front++;
}
}
else {
while (front <= n - 1) {
cout << queue[front] << " ";
front++;
}
front = 0;
while (front <= rear) {
cout << queue[front] << " ";
front++;
}
}cout << endl;
}
int main()
{
insert(5);
insert(2);
insert(3);
remove();
remove();
insert(6);
insert(3);
show();
return 0;
}