Bubble Sort

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

DATA STRUCTURE AND ALGORITHMS(LAB)

ASSIGNMENT NO 1

NAME : SAAD ALI MUBARAK

CLASS : BSCS 3

ROLL NO : 222201017

SUBMITTED TO : MA’AM RIDA BAJWA


QUESTION NO 2
#include<iostream>

#include<string>

#include<conio.h>

using namespace std;

struct node{

node* next;

node* prev;

int id;

string name;

};

class course{

node* head;

node* tail;

public:

course(){

head=NULL;

tail=NULL;
}

void insertathead(int data,string nam){

node* n=new node();

n->id=data;

n->name=nam;

if(head==NULL){

head=n;

tail=n;

tail->next=head;

return;

n->next=head;

head->prev=n;

head=n;

tail->next=n;

n->prev=tail;

void display(){

node* temp=head;

do{
cout<<"course id = "<<temp->id<<endl;

cout<<"course name = "<<temp->name<<endl;

temp=temp->next;

while(temp!=head);

cout<<endl;

void reverse(){

node* temp=tail;

do{

cout<<"course id = "<<temp->id<<endl;

cout<<"course name = "<<temp->name<<endl;

temp=temp->prev;

while(temp!=tail);

cout<<endl;
}

void insertattail(int data,string nam){

node* n=new node();

n->id=data;

n->name=nam;

if(head==NULL){

head=n;

tail=n;

tail->next=head;

head->prev=tail;

return;

tail->next=n;

n->prev=tail;

tail=n;

n->next=head;

head->prev=n;
}

void deleteathead(){

node* temp=head;

head=head->next;

tail->next=head;

head->prev=tail;

void deleteattail(){

node* temp=tail;

tail=tail->prev;

tail->next=head;

head->prev=tail;

int resize(){

node* temp=head;

int size=1;

do{

temp=temp->next;
size++;

}while(temp->next!=head);

return size;

void retrive(int index){

node* temp=head;

int size=1;

do{

if(index==size){

cout<<"course id="<<temp->id<<endl;

cout<<"course name="<<temp->name<<endl;

return;

temp=temp->next;

size++;

}while(temp!=head);

cout<<"invalid index\n";

bool check(int d){


if(head==NULL){

return false;

node* temp=head;

do{

if(d==temp->id){

return true;

temp=temp->next;

}while(temp!=head);

return false;

};

int main(){

course s;

int ch;
int i;

string n;

jo:

cout<<"Enter 1 to add course at front\n";

cout<<"Enter 2 to add course at back\n";

cout<<"Enter 3 to delete course from front\n";

cout<<"Enter 4 to delete course from back\n";

cout<<"Enter 5 to see size of list\n";

cout<<"Enter 6 to retrive at given index\n";

cout<<"Enter 7 to see list\n";

cout<<"Enter 8 to exit\n";

cin>>ch;

switch(ch){

case 1:

cout<<"Enter course id:";

cin>>i;

cout<<"Enter name of course:";

cin>>n;

if(s.check(i)){

cout<<"already exist\n";
}

else{

s.insertathead(i,n);

cout<<"Course is added\n";

goto jo;

break;

case 2:

cout<<"Enter course id:";

cin>>i;

cout<<"Enter name of course:";

cin>>n;

if(s.check(i)){

cout<<"already exist\n";

else{

s.insertattail(i,n);

cout<<"Course is added\n";

goto jo;
break;

case 3:

s.deleteathead();

cout<<"Course is deleted\n";

goto jo;

break;

case 4:

s.deleteattail();

cout<<"Course is deleted\n";

goto jo;

break;

case 5:

cout<<"List size ="<<s.resize()<<endl;

goto jo;

break;

case 6:

cout<<"Enter index:";

cin>>i;

s.retrive(i);
goto jo;

break;

case 7:

s.display();

goto jo;

break;

case 8:

exit(0);

break;

default:

cout<<"Invalid\n";

goto jo;

break;

OUTPUT:

You might also like