Name 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Name= Rabab Naseem

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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy