0% found this document useful (0 votes)
11 views

23-NTU-CS-1170(lab 6)

Uploaded by

mahaather45
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)
11 views

23-NTU-CS-1170(lab 6)

Uploaded by

mahaather45
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/ 14

1

National Textile University


Department of Computer
Science
Subject:
DSA
Submitted to:
Sir ARSAL
Submitted by:
Maha Ather

Reg number:
23-NTU-CS-1170
Lab no.:
6
2

Semester:3
Exercise 1:
Implement queue through arrays:
Code:
#include<iostream>
using namespace std;
const int size=10;
class queue{
private:
int array[size];
int front;
int rear;
public:
queue(){
front=-1;
rear=-1;
}
int getfront(){
if(isempty()){
cout<<"list is empty: "<<endl;
}
return array[front];
}
int getsize(){
3

return rear+1;
}
bool isfull(){
if(rear==size-1){
return true;
}else{
return false;
}
}
bool isempty(){
return front==-1;
}
void enque(int data){
if(isfull()){
cout<<"can't enter elemets: "<<endl;
}else{
array[++rear]=data;
if(front==-1){
front++;
}
}
}
void dequeue(){
if(isempty()){
cout<<"list is empty"<<endl;
4

}else{
front++;
if(front>rear) {
front=-1;
rear=-1;
}
}
}
void print(){
if(isempty()==true){
cout<<"the queue is empty"<<endl;
}else{
for(int i=front;i<=rear;i++){
cout<<array[i]<<endl;
}
}
}
};
int main(){
queue* qw=new queue();
cout<<"data insertion in queue: "<<endl;
qw->enque(100);
qw->enque(200);
qw->enque(300);
qw->enque(400);
5

qw->print();
cout<<"after data deletion: "<<endl;
qw->dequeue();
qw->print();
cout<<"front element is: "<<endl;
cout<<qw->getfront();
return 0;
}
Output:

Exercise 2
Implement queue through linked list:
Code:
#include<iostream>
using namespace std;
6

//through linked list


class node{
private:
int data;
node* next;
public:
//setter
void setdata(int data){
this->data=data;
}
void setnext(node* next){
this->next=next;
}
//getter
int getdata(){
return data;
}
node* getnext(){
return next;
}
};
class listqueue{
private:
node* front;
node* rear;
7

public:
listqueue(){
front=nullptr;
rear=nullptr;
}
bool isempty(){
if(front==NULL){
return true;
}else{
return false;
}
}
int getfront(){
return front->getdata();
}
void enqueue(int data){
node* newnode=new node();
newnode->setdata(data);
newnode->setnext(nullptr);
if(isempty()==true){
front=newnode;
rear=newnode;
}else{
rear->setnext(newnode);
rear=newnode;
8

}
}
void dequeue(){
node* temp=front;
front=front->getnext();
delete temp;
}
void display(){
node* temp=front;
if(isempty()==true){
cout<<"queue is empty";
return ;
}
while(temp!=NULL){
cout<<temp->getdata()<<"->";
temp=temp->getnext();
}
cout<<"NULL";
}
};
int main(){
//queue through linked list
listqueue* li=new listqueue();
cout<<"insertion of data: "<<endl;
li->enqueue(100);
9

li->enqueue(200);
li->enqueue(300);
li->enqueue(400);
li->display();
cout<<endl;
cout<<"queue after dletion: "<<endl;
li->dequeue();
li->display();
cout<<endl;
cout<<"front element is: "<<endl;
cout<<li->getfront();
return 0;
}

Output:
10

Exercise 3:
Write a menu based program which should implement Queue by
using array with following options.
1. Insert Element into Queue (Press 1)
2. Delete element from queue (Press 2)
3. Display the queue (Press 3)
4. Exit(Press 4)
Code:
#include<iostream>
using namespace std;
const int size=10;
class queue{
private:
int array[size];
int front;
int rear;
public:
queue(){
front=-1;
rear=-1;
}
int getfront(){
if(isempty()){
cout<<"list is empty: "<<endl;
}
11

return array[front];
}
int getsize(){
return rear+1;
}
bool isfull(){
if(rear==size-1){
return true;
}else{
return false;
}
}
bool isempty(){
return front==-1;
}
void enque(int data){
if(isfull()){
cout<<"can't enter elemets: "<<endl;
}else{
array[++rear]=data;
if(front==-1){
front++;
}
}
}
12

void dequeue(){
if(isempty()){
cout<<"list is empty"<<endl;
}else{
cout<<"Element " << array[front] << " deleted
from the queue." << endl;
front++;
if(front>rear) {
front=-1;
rear=-1;
}
}
}
void print(){
if(isempty()==true){
cout<<"the queue is empty"<<endl;
}else{
for(int i=front;i<=rear;i++){
cout<<array[i]<<endl;
}
}
}
};
int main(){
queue* qw=new queue();
13

int choice;
int data;
do{
cout<<"press 1 for data insertion "<<endl;
cout<<"press 2 for data deletion"<<endl;
cout<<"press 3 for data display"<<endl;
cout<<"press 4 for Exit"<<endl;
cout<<"enter choice: "<<endl;
cin>>choice;
switch(choice){
case 1:
cout<<"enter elemet to insert"<<endl;
cin>>data;
qw->enque(data);
cout<<"element "<<data<<" is inserted in
queue";
break;
case 2:
qw->dequeue();
case 3:
cout<<"data in queue is: "<<endl;
qw->print();
break;
case 4:
cout<<"exiting program"<<endl;
14

return 0;
}
}while(choice!=4);
return 0;
}
Output:

…………………………….THE END……………………………..

You might also like