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

Algorithm Lab Manual Full

Uploaded by

Getaneh Awoke
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Algorithm Lab Manual Full

Uploaded by

Getaneh Awoke
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 122

A.

Searching
/*

Example - 1

A linear search code which accept n number of integers and a number

which is to be searched from the user . It searches the number in the list

and then displays whether it is in the list or not */

#include<iostream>

using namespace std;

void linear(int x[],int key,int size)

int index=0,found=0;

do{

if (x[index]==key)

found=1;

else

index++;

}while(found==0&&index<size);

if(found==1)

cout<<key<<" is found in the list\n";

else

cout<<key<< " is not found\ in the listn";

}
int main()

int list[6],i,num,n;

cout<<" how many numbers do you wa to enter \n";

cin>>n;

for(i=0;i<n;i++)

cin>>list[i];

cout<<"enter the number to be searched\n";

cin>>num;

linear(list,num,n);

=====================================
The output of the above code is the following

enter six numbers


2
1
5
8
4
8
enter the number to be searched
6
6 is not found in the list
/*
Example - 2

A linear search code which accept n number ofof names and a name

which is to be searched from the user . It searches the name in the list and

then displays whether it is in the list or not

*/

#include<iostream>

#include<string>

using namespace std;

void linear(string x[],string key)

int index=0,found=0;

do{

if(x[index]==key)

found=1;

else

index++;

}while(found==0&&index<6);

if(found==1)

cout<<key<<" is found in the list\n";

else

cout<<key<< " is not found in the list\n";


}

int main()

string list[6],name;

int i;

cout<<" enter 6 names\n";

for(i=0;i<6;i++)

cin>>list[i];

cout<<"enter the name to be searched\n";

cin>>name;

linear(list,name);

=====================================
The output of the above code is the following

enter six names


abebe
kebede
taye
sara
lily
dagne
enter the number to be searched
lily
lily is found in the list
/*
Example - 3

A binary search code which accept n number of integers and a number

which is to be searched from the user . It searches the number in the list

and then displays whether it is in the list or not

*/

#include<iostream>

using namespace std;

void binary(int x[],int key)

int left=0,found=0,right=5,mid;

do{

mid=(left+right)/2;

if(x[mid]==key)

found=1;

else{

if(key<x[mid])

right=mid-1;

else

left=mid+1;

}while(found==0&&left<=right);
if(found==1)

cout<<key<<" is found in the list\n";

else

cout<<key<< " is not found in the list\n";

int main(){

int list[6],i,num;

cout<<" enter six ascendinglly sorted numbers\n";

for(i=0;i<6;i++)

cin>>list[i];

cout<<"enter the number to be searched\n";

cin>>num;

binary(list,num);

}
=====================================
The output of the above code is the following
=====================================
enter six ascendinglly sorted numbers
2
4
5
8
11
15
enter the number to be searched
5
5 is found in the list
/*
Example - 4

binary search in descending order


#include<iostream>

using namespace std;

void binary(int x[],int key)

int left=0,found=0,right=5,mid;

do{

mid=(left+right)/2;

if(x[mid]==key)

found=1;

else

if(key<x[mid])

left=mid+1;

else

right=mid-1;

}while(found==0&&left<=right);

if(found==1)
cout<<key<<" is found in the list\n";

else

cout<<key<< " is not found\n";

int main()

int list[6],i,num;

cout<<" enter 6 numbers in descending order\n";

for(i=0;i<6;i++)

cin>>list[i];

cout<<"enter the key\n";

cin>>num;

binary(list,num);

}
=====================================
The output of the above code is the following
=====================================
enter six numbers in descending order
9
7
5
3
1
0
enter the number to be searched
6
6 is not found in the list
/*

Example - 5

A binary search code which accept n number ofof names and a name

which is to be searched from the user . It searches the name in the list and

then displays whether it is in the list or not

*/

#include<iostream>

#include<string>

using namespace std;

void binary(string x[],string key)

int left=0,found=0,right=5,mid;

do{

mid=(left+right)/2;

if(x[mid]==key)

found=1;

else

if(key<x[mid])

right=mid-1;

else

left=mid+1;
}

}while(found==0&&left<=right);

if(found==1)

cout<<key<<" is found in the list\n";

else

cout<<key<< " is not found\n";

int main(){

string list[6],name;

int i;

cout<<" enter sorted 6 names in ascending order\n";

for(i=0;i<6;i++)

cin>>list[i];

cout<<"enter the key\n";

cin>>name;

binary(list,name);

}
=====================================
The output of the above code is the following
=====================================
enter six names in ascending order
abebe
kebede
sisay
taye
walelign
zufan
enter the name to be searched
kemal
kemal is found in the list

/*

Example - 6

A binary search code which accept n number of integers.

and a number which is to be searched from the user . It searches the

number in the list and then displays whether it is in the list or not .

The program sorts the numbers automatically while the user enters each

number .

*/

#include<iostream>

using namespace std;

void binary(int x[],int key)

int left=0,found=0,right=5,mid;

do{

mid=(left+right)/2;

if(x[mid]==key)
found=1;

else

if(key<x[mid])

right=mid-1;

else

left=mid+1;

}while(found==0&&left<=right);

if(found==1)

cout<<key<<" is found in the list\n";

else

cout<<key<< " is not found\n";

int main()

int num, i,j,x[6];

int y[6];

for(i=0;i<6;i++)

cout<<"enter a numbers\n";

cin>>num;
y[i]=num;

if(i==0)

x[i]=num;

for(j=i-1;j>=0;j--)

if(num<x[j])

x[j+1]=x[j];

x[j]=num;

else

x[j+1]=num;

break;

int k;

cout<<"enter a number to be searched\n";

cin>>k;

cout<<"the numbers before sorted\n";

for(i=0;i<6;i++)
cout<<y[i]<<" , ";

cout<<endl;

cout<<"the numbers after sorted\n";

for(i=0;i<6;i++)

cout<<x[i]<<" , ";

cout<<endl;

binary(x,k);

=====================================

The output of the above code is the following

=====================================

enter six numbers

9
7

5
3
1
0
The numbers before sorted
9,7,5,3,1,0
The numbers after sorted
0,1,3,5,7,9

enter the number to be searched


5

5 is found in the list


//---7---

*/a binary search code which guide the users to enter sorted numbers.if

the user entered unsorted numbers the program asks the user to re- enter

sorted numbers*/

#include<iostream>

using namespace std;

void binary(int x[],int key)

int left=0,found=0,right=5,mid;

int index;

do{

mid=(left+right)/2;

if(x[mid]==key)

found=1;

//index=mid;

else
{

if(key<x[mid])

right=mid-1;

else

left=mid+1;

}while(found==0&&left<=right);

if(found==1)

cout<<key<<" is found in the list at index of "<<mid<<"\n";

else

cout<<key<< " is not found\n";

int main()

int num, i,j,x[6];

int y[6];

for(i=0;i<6;i++)

add:

cout<<"enter a numbers\n";

cin>>num;

y[i]=num;
if(i==0)

x[i]=num;

else

if(num<x[i-1])

cout<<" please re-enter a number which is greater than. "<<x[i-1]<<endl;

{goto add;}

else

x[i]=num;

int k;

cout<<"enter a number to be searched\n";

cin>>k;

for(i=0;i<6;i++)

cout<<x[i]<<",";

cout<<endl;

binary(x,k);

}
=====================================

The output of the above code is the following

=====================================

enter six numbers in ascending order


3

please re-enter a number which is greater than 3

please re-enter a number which is greater than 7


9

11

15

enter the number to be searched

6 is not found in the list

B. Sorting
//---8---

*/a bubble sort code that accepts unsorted numbers from the keyboard

and it sorts the numbers in ascending order then it displays them before

and after sorted*/

#include <iostream>

using namespace std;

void bubble(int x[])

int i,j,temp;

for(i=0;i<6;i++)

for(j=5;j>i;j--)

if(x[j]<x[j-1])

temp=x[j];

x[j]=x[j-1];

x[j-1]=temp;

}}for(int k=0;k<6;k++)

cout<<x[k]<<" , ";

cout<<endl;}

cout<<"the numbers after sorted\n";


for(i=0;i<6;i++)

cout<<x[i]<<" , ";

int main(){

int y[6],i;

cout<<"enter 6 unsorted numbers\n";

for(i=0;i<6;i++)

cin>>y[i];

cout<<"the numbers before sorted\n";

for(i=0;i<6;i++)

cout<<y[i]<<" , ";

cout<<endl;

bubble(y);

=====================================

The output of the above code is the following

=====================================

enter six unsorted numbers

9
7

5
0
1
3
The numbers before sorted
9,7,5,0,1,3
The numbers after sorted
0,1,3,5,7,9

//---9---

*/a bubble sort code that accepts unsorted numbers from the keyboard

and it sorts the numbers in descending order then it displays them before

and after sorted*/

#include <iostream>

using namespace std;

void bubble(int x[])

int i,j,temp;

for(i=0;i<6;i++)

for(j=5;j>i;j--)

if(x[j]<x[j-1])

temp=x[j];

x[j]=x[j-1];

x[j-1]=temp;

}}for(int k=0;k<6;k++)

cout<<x[k]<<" , ";
cout<<endl;}

cout<<"the numbers after sorted\n";

for(i=0;i<6;i++)

cout<<x[i]<<" , ";

int main()

int y[6],i;

cout<<"enter 6 unsorted numbers\n";

for(i=0;i<6;i++)

cin>>y[i];

cout<<"the numbers before sorted\n";

for(i=0;i<6;i++)

cout<<y[i]<<" , ";

cout<<endl;

bubble(y);

=====================================

The output of the above code is the following

=====================================

enter six unsorted numbers


4
7
5
0
1
3
The numbers before sorted
4,7,5,0,1,3
The numbers after sorted
7,5,4,3,1,0

//---10---

*/a bubble sort code that accepts unsorted n numbers of names from the

keyboard and it sorts the names in descending order then it displays them

before and after sorted*/

//in descending order

#include <iostream>

#include<string>

using namespace std;

void bubble(string x[])

string temp;

int i,j;

for(i=0;i<6;i++)

for(j=5;j>i;j--)

{
if(x[j]>x[j-1])

temp=x[j];

x[j]=x[j-1];

x[j-1]=temp;

}}

for(int k=0;k<6;k++)

cout<<x[k]<<" , ";

cout<<endl;

cout<<"the names after sorted in descending order\n";

for(i=0;i<6;i++)

cout<<x[i]<<" , ";

int main()

string x[6];

int i;

cout<<"enter 6 unsorted names\n";

for(i=0;i<6;i++)

cin>>x[i];
cout<<"the names before sorted\n";

for(i=0;i<6;i++)

cout<<x[i]<<" , ";

cout<<endl;

bubble(x);

=====================================

The output of the above code is the following

=====================================

enter six unsorted names


abebe
belay
zelalem
chala
martha
jemal
tsion
masresha
The names before sorted
abebe
belay
zelalem
chala
martha
jemal
tsion
masresha

The names after sorted


zelalem
tsion
masresha
jemal
chala
belay
abebe

//---11---

*/a insertion sort code that accepts unsorted numbers from the keyboard

and it sorts the numbers in ascending order then it displays them before

and after sorted*/

#include <iostream>

using namespace std;

void insertion(int x[])

int i,j,temp;

for(i=1;i<6;i++)

temp=x[i];

for(j=i;j>0&&temp<x[j-1];j--)

x[j]=x[j-1];

x[j-1]=temp;

}
for(int k=0;k<6;k++)

cout<<x[k]<<" , ";

cout<<endl;

cout<<"the numbers after using insertion sort\n";

for(i=0;i<6;i++)

cout<<x[i]<<" , ";

int main()

int y[6];

int i;

cout<<"enter 6 unsorted numbers\n";

for(i=0;i<6;i++)

cin>>y[i];

cout<<"the numbers before sorted\n";

for(i=0;i<6;i++)

cout<<y[i]<<" , ";

cout<<endl;

insertion(y);

}
=====================================

The output of the above code is the following

=====================================

enter six unsorted numbers

4
7

5
0
1
3

The numbers before sorted

4,7,5,0,1,3

The numbers after using insertion sort

7,5,4,3,1,0

//---12---

*/a insertion sort code that accepts unsorted n numbers of names from the

keyboard and it sorts the names in descending order then it displays them

before and after sorted*/


#include <iostream>

#include<string>

using namespace std;

void insertion(string x[])

int i,j;

string temp;

for(i=1;i<6;i++)

temp=x[i];

for(j=i;j>0&&temp>x[j-1];j--)

x[j]=x[j-1];

x[j-1]=temp;

for(int k=0;k<6;k++)

cout<<x[k]<<" , ";

cout<<endl;

cout<<"the names after sorted using insertion sort\n";


for(i=0;i<6;i++)

cout<<x[i]<<" , ";

int main()

string y[6];

int i;

cout<<"enter 6 unsorted names\n";

for(i=0;i<6;i++)

cin>>y[i];

cout<<"the names before sorted\n";

for(i=0;i<6;i++)

cout<<y[i]<<" , ";

cout<<endl;

insertion(y);

=====================================

The output of the above code is the following

=====================================

enter six unsorted names


abebe
belay
zelalem
chala
martha
jemal
tsion
masresha
The names before sorted
abebe
belay
zelalem
chala
martha
jemal
tsion
masresha

The names after sorted using insertion sort


zelalem
tsion
masresha
jemal
chala
belay
abebe

//---13---

*/a insertion sort code that accepts unsorted numbers from the keyboard

and it sorts the numbers in descending order then it displays them before

and after sorted*/

#include <iostream>

using namespace std;


void insertion(int x[])

int i,j,temp;

for(i=1;i<6;i++)

temp=x[i];

for(j=i;j>0&&temp>x[j-1];j--)

x[j]=x[j-1];

x[j-1]=temp;

for(int k=0;k<6;k++)

cout<<x[k]<<" , ";

cout<<endl;

cout<<"the numbers after sorted using insertion sort in descending

order\n";

for(i=0;i<6;i++)

cout<<x[i]<<" , ";

}
int main()

int y[6],i;

cout<<"enter 6 unsorted numbers\n";

for(i=0;i<6;i++)

cin>>y[i];

cout<<"the numbers before sorted\n";

for(i=0;i<6;i++)

cout<<y[i]<<" , ";

cout<<endl;

insertion(y);

=====================================

The output of the above code is the following

=====================================

enter six unsorted numbers

4
7
5
0
1
3

The numbers before sorted

4,7,5,0,1,3

The numbers after using insertion sort in descending order

7,5,4,3,1,0

//---14-----

*/a selection sort code that accepts unsorted numbers from the keyboard

and it sorts the numbers in ascending order then it displays them before

and after sorted*/

#include <iostream>

#include<string>

using namespace std;

void selection(string x[])

{
int i,j,small;

string temp;

for(i=0;i<6;i++)

small=i;

for(j=i;j<6;j++)

if(x[j]<x[small])

small=j;

temp=x[i];

x[i]=x[small];

x[small]=temp;

for(int k=0;k<6;k++)

cout<<x[k]<<" , ";

cout<<endl;

cout<<"the names after sorted using selection sort in ascending order\n";

for(i=0;i<6;i++)

cout<<x[i]<<" , ";

int main()
{

string y[6];

int i;

cout<<"enter 6 unsorted names\n";

for(i=0;i<6;i++)

cin>>y[i];

cout<<"the names before sorted\n";

for(i=0;i<6;i++)

cout<<y[i]<<" , ";

cout<<endl;

selection(y);

=====================================

The output of the above code is the following

=====================================

enter six unsorted numbers

9
7

5
0
1
3
The numbers before sorted

9,7,5,0,1,3

The numbers after sorted using selection sort in ascending order

0,1,3,5,7,9

//---15---

#include <iostream>

#include<string>

using namespace std;

int main()

string num;

int i,j;

string x[6];

string y[6];

i=0;

while(i<6)

cout<<"enter a name\n";

cin>>num;

y[i]=num;
if(i==0)

x[i]=num;

j=i-1;

while(j>=0)

if(x[j]<num)

x[j+1]=num;

break;

else

x[j+1]=x[j];

x[j]=num;

j--;

for(int k=0;k<=i;k++)

cout<<x[k]<<" ";

cout<<endl;

i++;

}
cout<<"the names before sorted\n";

for(i=0;i<6;i++)

cout<<y[i]<<" , ";

cout<<endl;

cout<<"the names after sorted\n";

for(i=0;i<6;i++)

cout<<x[i]<<" , ";

//---16---

#include <iostream>

#include<string>

using namespace std;

void selection(char x[])

int i,j,small;

char temp;

for(i=0;i<6;i++)

small=i;

for(j=i;j<6;j++)

if(x[j]<x[small])
small=j;

temp=x[i];

x[i]=x[small];

x[small]=temp;

for(int k=0;k<6;k++)

cout<<x[k]<<" , ";

cout<<endl;

cout<<"the letter after sorted using selection sort\n";

for(i=0;i<6;i++)

cout<<x[i]<<" , ";

int main()

char y[6];

int i;

cout<<"enter 6 unsorted character s\n";

for(i=0;i<6;i++)

cin>>y[i];

cout<<"the characters before sorted\n";

for(i=0;i<6;i++)
cout<<y[i]<<" , ";

cout<<endl;

selection(y);

C. Single linked list


//---17---

// add at the end

#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;

};

int count=0;

void display();

struct node *start=NULL;

void add_end()

char ask;
do{

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

temp->nxt=NULL;

if(start==NULL)

start= temp;

else

node *target=start;

while(target->nxt!= NULL)

target=target->nxt;

target->nxt=temp;

count++;

cout<<count<<"nodes has the list\n";

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

}
void display()

node *temp;

if(start==NULL)

cout<<"start-->null\n";

else

cout<<"start-->";

temp=start;

do{

cout<<temp->num<<"-->";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"null\n";

}}

int main()

add_end();

//---18---
// add in the middle
#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;

};

int count=0;

void display();

struct node *start=NULL;

void addmiddle()

char ask;

int place;

int i;

do{

node *temp1;

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

if(start==NULL)
{

temp->nxt=NULL;

start= temp;

count=1;

cout<<count<<"nodes has the list\n";

display();

else{

cout<<"After which node do u want to insert it\n";

cin>>place;

if(place>count)

do{

cout<<"there is only "<<count<<" nodes . please enter again\n";

cin>>place;

}while(place>count);

temp1= start;

for(i=1;i!=place;i++)

temp1=temp1->nxt;

temp->nxt=temp1->nxt;

temp1->nxt= temp;

count++;
cout<<count<<"nodes has the list\n";

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void display()

node *temp;

if(start==NULL)

cout<<"start-->null\n";

else

cout<<"start-->";

temp=start;

do{

cout<<temp->num<<"-->";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"null\n";

}}
int main()

addmiddle();

//---19---
//delete anywhere

#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;

};

int count=0;

void display();

struct node *start=NULL;

void deleteanywhere()

char ask;

int place;

int i;

int del;
do{

node *temp1;

node *target;

if(start==NULL)

cout<<"The list has "<<count<<" nodes has the list\n";

display();

else{

cout<<" which node do u want to delete it?\n";

cin>>place;

if(place>count)

do{

cout<<"there is only "<<count<<" nodes . please enter again\n";

cin>>place;

}while(place>count);

target= start;

if(target->nxt==NULL || place==1)

del=target->num;

start=target->nxt;
delete target;

cout<<del<<" is deleted succefully\n";

count--;

else

for(i=1;i!=place;i++)

temp1=target;

target=target->nxt;

temp1->nxt=target->nxt;

del=target->num;

delete target;

cout<<del<<" is deleted succefully\n";

count--;

cout<<count<<"nodes has the list\n";

display();

cout<<"Do u want to delete again(y/n)\n";

cin>>ask;
}while(ask=='y'||ask=='Y');

void add_end()

char ask;

do{

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

temp->nxt=NULL;

if(start==NULL)

start= temp;

else

node *target=start;

while(target->nxt!= NULL)

target=target->nxt;

target->nxt=temp;

count++;

cout<<count<<"nodes has the list\n";


display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void display()

node *temp;

if(start==NULL)

cout<<"start-->null\n";

else

cout<<"start-->";

temp=start;

do{

cout<<temp->num<<"-->";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"null\n";

}}

int main()

{
add_end();

deleteanywhere();

//---20 ----

//delete end

#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;

};

int count=0;

void display();

struct node *start=NULL;

void deleteEnd()

int del;

char ask;

do{

node *temp;

if(start==NULL)
cout<<"the list is empty\n";

else

temp=start;

if(temp->nxt==NULL)

del=temp->num;

start=NULL;

delete temp;

cout<<del<<" is deleted\n";

else

node *target;

while(temp->nxt!= NULL)

target=temp;

temp=temp->nxt;

del= temp->num;

target->nxt= NULL;

delete temp;
cout<<del<<" is deleted\n";

count--;

cout<<count<<" : nodes has the list\n";

display();

cout<<"Do u want to delete again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void add_start()

char ask;

do{

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

if(start==NULL)

start= temp;

temp->nxt=NULL;
}

else

temp->nxt=start;

start= temp;

count++;

cout<<count<<" : nodes has the list\n";

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void display()

node *temp;

if(start==NULL)

cout<<"start-->null\n";

else

cout<<"start-->";
temp=start;

do{

cout<<temp->num<<"-->";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"null\n";

}}

int main ()

add_start();

deleteEnd();

//---21---
//delete start

#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;

};
int count=0;

void display();

struct node *start=NULL;

void deleteStart()

int del;

char ask;

do{

node *temp;

if(start==NULL)

cout<<"the list is empty\n";

else

temp=start;

if(temp->nxt==NULL)

del=temp->num;

start=NULL;

delete temp;

cout<<del<<" is deleted\n";

else
{

start=temp->nxt;

del= temp->num;

delete temp;

cout<<del<<" is deleted\n";

}}

count--;

cout<<count<<" : nodes has the list\n";

display();

cout<<"Do u want to delete again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void add_start()

char ask;

do{

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

if(start==NULL)
{

start= temp;

temp->nxt=NULL;

else

temp->nxt=start;

start= temp;

count++;

cout<<count<<" : nodes has the list\n";

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void display()

node *temp;

if(start==NULL)

cout<<"start-->null\n";
else

cout<<"start-->";

temp=start;

do{

cout<<temp->num<<"-->";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"null\n";

}}

int main()

add_start();

deleteStart();

//--22---

//sort

#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;
};

int count=0;

void display();

struct node *start=NULL;

void sort()

int temp,ch;

node *t1,*t2,*t3,*t4;

cout<<"The numbers before sorted\n";

display();

int i;

for(t1=start;t1!=NULL ;t1=t1->nxt)

for(t3=start,t2=t3->nxt;t2!=NULL;t3=t2,t2=t2->nxt)

if(t3->num>t2->num)

if(start==t3)

start=t2;

t3->nxt=t2->nxt;

t2->nxt= t3;
t1=start;

else

t4=start;

while(t4->nxt!=t3)

t4=t4->nxt;

t3->nxt=t2->nxt;

t2->nxt=t4->nxt;

t4->nxt=t2;

cout<<"The numbers after sorted in ascending order\n";

display();

void add_start()

char ask;

do{

node *temp;
temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

if(start==NULL)

start= temp;

temp->nxt=NULL;

else

temp->nxt=start;

start= temp;

count++;

cout<<count<<" : nodes has the list\n";

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void display()
{

node *temp;

if(start==NULL)

cout<<"start-->null\n";

else

cout<<"start-->";

temp=start;

do{

cout<<temp->num<<"-->";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"null\n";

}}

int main()

add_start();

sort();

//---23---

/* A single linked list code which performs all the following tasks in one

program
1. add nodes at the end

2. add nodes at the start

3. delete nodes at the end

4. delete nodes at the start

5. add nodes somewhere in the middle

6.delete nodes somewhere in the middle

7. sort the lists

8. display the lists*/

#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;

};

int count=0;

void display();

struct node *start=NULL;

void add_end()

char ask;

do{

node *temp;
temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

temp->nxt=NULL;

if(start==NULL)

start= temp;

else

node *target=start;

while(target->nxt!= NULL)

target=target->nxt;

target->nxt=temp;

count++;

cout<<count<<"nodes has the list\n";

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void addmiddle()

{
char ask;

int place;

int i;

do{

node *temp1;

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

if(start==NULL)

temp->nxt=NULL;

start= temp;

count=1;

cout<<count<<"nodes has the list\n";

display();

else{

cout<<"After which node do u want to insert it\n";

cin>>place;

if(place>count)

do{
cout<<"there is only "<<count<<" nodes . please enter again\n";

cin>>place;

}while(place>count);

temp1= start;

for(i=1;i!=place;i++)

temp1=temp1->nxt;

temp->nxt=temp1->nxt;

temp1->nxt= temp;

count++;

cout<<count<<"nodes has the list\n";

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void deleteanywhere()

char ask;

int place;
int i;

int del;

do{

node *temp1;

node *target;

if(start==NULL)

cout<<"The list has "<<count<<" nodes has the list\n";

display();

else{

cout<<" which node do u want to delete it?\n";

cin>>place;

if(place>count)

do{

cout<<"there is only "<<count<<" nodes . please enter again\n";

cin>>place;

}while(place>count);

target= start;

if(target->nxt==NULL || place==1)

{
del=target->num;

start=target->nxt;

delete target;

cout<<del<<" is deleted succefully\n";

count--;

else

for(i=1;i!=place;i++)

temp1=target;

target=target->nxt;

temp1->nxt=target->nxt;

del=target->num;

delete target;

cout<<del<<" is deleted succefully\n";

count--;

cout<<count<<"nodes has the list\n";

display();

}
cout<<"Do u want to delete again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void add_start()

char ask;

do{

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

if(start==NULL)

start= temp;

temp->nxt=NULL;

else

temp->nxt=start;

start= temp;

}
count++;

cout<<count<<" : nodes has the list\n";

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void deleteEnd()

int del;

char ask;

do{

node *temp;

if(start==NULL)

cout<<"the list is empty\n";

else

temp=start;

if(temp->nxt==NULL)

del=temp->num;

start=NULL;
delete temp;

cout<<del<<" is deleted\n";

else

node *target;

while(temp->nxt!= NULL)

target=temp;

temp=temp->nxt;

del= temp->num;

target->nxt= NULL;

delete temp;

cout<<del<<" is deleted\n";

count--;

cout<<count<<" : nodes has the list\n";

display();

cout<<"Do u want to delete again(y/n)\n";

cin>>ask;
}while(ask=='y'||ask=='Y');

void deleteStart()

int del;

char ask;

do{

node *temp;

if(start==NULL)

cout<<"the list is empty\n";

else

temp=start;

if(temp->nxt==NULL)

del=temp->num;

start=NULL;

delete temp;

cout<<del<<" is deleted\n";

else

{
start=temp->nxt;

del= temp->num;

delete temp;

cout<<del<<" is deleted\n";

}}

count--;

cout<<count<<" : nodes has the list\n";

display();

cout<<"Do u want to delete again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void sort()

int temp,ch;

node *t1,*t2,*t3,*t4;

cout<<"The numbers before sorted\n";

display();

int i;

for(t1=start;t1!=NULL ;t1=t1->nxt)

for(t3=start,t2=t3->nxt;t2!=NULL;t3=t2,t2=t2->nxt)
{

if(t3->num>t2->num)

if(start==t3)

start=t2;

t3->nxt=t2->nxt;

t2->nxt= t3;

t1=start;

else

t4=start;

while(t4->nxt!=t3)

t4=t4->nxt;

t3->nxt=t2->nxt;

t2->nxt=t4->nxt;

t4->nxt=t2;

}
cout<<"The numbers after sorted in ascending order\n";

display();

void display()

node *temp;

if(start==NULL)

cout<<"start-->null\n";

else

cout<<"start-->";

temp=start;

do{

cout<<temp->num<<"-->";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"null\n";

}}

int main()

int ch;

char ask;
menu:

cout<<"enter 1 to add at end\n";

cout<<"enter 2 to add at start\n";

cout<<"enter 3 to delete at the end\n";

cout<<"enter 4 to delete at start\n";

cout<<"enter 5 to display\n";

cout<<"enter 6 to add in the middle\n";

cout<<"enter 7 to delete in the middle\n";

cout<<"enter 8 to sort nodes\n";

cout<<"enter 9 to exit\n";

cin>>ch;

if(ch==1)

add_end();

{goto menu;}

else if(ch==2)

add_start();

{goto menu;}

else if(ch==3)
{

deleteEnd();

{goto menu;}

else if (ch==4)

deleteStart();

{goto menu;}

else if(ch==5)

display();

{goto menu;}

else if(ch==6)

addmiddle();

{goto menu;}

else if(ch==7)

deleteanywhere();
{goto menu;}

else if(ch==8)

sort();

{goto menu;}

else if(ch==9)

cout<<"good bye\n";

else

cout<<"invalid choise\n";

D. double linked list


//---24---

#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;

node *prv;

};
struct node *start=NULL;

node *current;

void display();

void addend()

char ask;

do{

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

temp->nxt=NULL;

if(start==NULL)

start= temp;

current=temp;

current->nxt=NULL;

current->prv=NULL;

else

node *target=current;
while(target->nxt!= NULL)

target=target->nxt;

target->nxt=temp;

temp->prv=target;

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void deleteend()

char ask;

int del;

do{

node *temp;

node *target=current;

if(start==NULL)

cout<<"the list is empty\n";

else if(target->nxt==NULL&& current->prv==NULL)

{
del=target->num;

start=NULL;

current->nxt=NULL;

current->prv=NULL;

cout<<del<<" is deleted successfully\n";

else if(target->nxt==NULL && target->prv!=NULL)

del=target->num;

current=current->prv;

current->nxt=NULL;

delete target;

cout<<del<<" is deleted successfully\n";

else

while(target->nxt!= NULL)

temp=target;

target=target->nxt;

del= target->num;
temp->nxt=NULL;

delete target;

cout<<del<<" is deleted succsefully\n";

display();

cout<<"Do u want to delete again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void deletestart()

char ask;

int del;

do{

node *temp;

node *target=current;

if(start==NULL)

cout<<"the list is empty\n";

else if(target->prv==NULL&& current->nxt==NULL)

del=target->num;
start=NULL;

current->nxt=NULL;

current->prv=NULL;

cout<<del<<" is deleted successfully\n";

else if(target->prv==NULL && target->nxt!=NULL)

del=target->num;

current=current->nxt;

current->prv=NULL;

delete target;

cout<<del<<" is deleted successfully\n";

else

while(target->prv!= NULL)

temp=target;

target=target->prv;

del= target->num;

temp->prv=NULL;
delete target;

cout<<del<<" is deleted succsefully\n";

display();

cout<<"Do u want to delete again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void addstart()

char ask;

do{

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

temp->prv=NULL;

if(start==NULL)

start= temp;

current=temp;

current->nxt=NULL;
current->prv=NULL;

else

node *target=current;

while(target->prv!= NULL)

target=target->prv;

target->prv=temp;

temp->nxt=target;

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void display()

node *temp;

if(start==NULL)

cout<<"NULL<---(current)--->NULL\n";

else
{

cout<<"NULL<---";

temp=current;

while(temp->prv!=NULL)

temp=temp->prv;

do{

if(temp==current)

cout<<"("<<temp->num<<")<==>";

else

if(temp->nxt==NULL)

cout<<temp->num<<"--->";

else

cout<<temp->num<<"<==>";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"NULL\n";

}}

int main()

int ch;
menu:

cout<<"enter 1 to add end \n";

cout<<"enter 2 to add start\n";

cout<<"Enter 3 to delete at the end\n";

cout<<"Enter 4 to delete at the start\n";

cout<<"enter 5 to display\n";

cout<<" enter 6 to exit\n";

cin>>ch;

if(ch==1)

addend();

{goto menu;}

else if(ch==2)

addstart();

{goto menu;}

else if(ch==3)

deleteend();

{goto menu;}
}

else if(ch==4)

deletestart();

{goto menu;}

else if(ch==5)

display();

{goto menu;}

else if(ch==6)

cout<<"good bye\n";

else

cout<<"invalid choice\n";

#include <iostream>

using namespace std;

static int stack[4];

int top=-1;

void push(int x)
{

if(top<3)

top ++;

stack[top]=x;

else

cout<<"stack is overflow\n";

void pop ()

int del;

if(top==-1)

cout<<"stack is underflow\n";

else

del=stack[top];

stack[top]==NULL;

top--;

cout<<del<<" is deleted\n";

}
void display()

int i;

cout<<"value\t index\n";

cout<<"===== =====\n";

for(i=0;i<4;i++)

if(stack[i]==NULL)

cout<<"[ ]\t "<<i;

else

cout<<"[";

cout<<stack[i]<<"]\t "<<i;

if(i==top)

cout<<"<---top";

cout<<endl;

int main(){

int n,ch;

char ask;
menu:

cout<<"enter 1 to push\n";

cout<<"enter 2 to pop\n";

cout<<"enter 3 to display\n";

cout<<"enter 4 to exit\n";

cin>>ch;

if(ch==1)

add:

cout<<"enter a number\n";

cin>>n;

push(n);

display();

cout<<"do u want to push again? (y/n)\n";

cin>>ask;

if(ask=='y'||ask=='Y')

{goto add;}

else

{goto menu;}

else if(ch==2)

{
del:

pop();

display();

cout<<"do u want to pop again? (y/n)\n";

cin>>ask;

if(ask=='y'||ask=='Y')

{goto del;}

else

{goto menu;}

else if(ch==3)

display();

else if(ch==4)

cout<<"good bye";

else

cout<<"invalid choise\n";

cout<<"pls re-enter again \n";

{goto menu;}

return 0;

}
//---25---

#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;

};

void display();

struct node *start=NULL;

node *top=NULL;

void push()

char ask;

do{

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

temp->nxt=NULL;

if(start==NULL)

start= temp;
top=start;

else

top->nxt =temp;

top=temp;

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void pop()

char ask;

int del_val;

do{

if(start==NULL)

cout<<"the stack is empty \n";

else

if(start->nxt==NULL)
{

del_val=top->num;

delete top;

start=NULL;

top=start;

else

node *temp=start;

while(temp->nxt!=top)

temp=temp->nxt;

del_val=top->num;

temp->nxt =NULL;

delete top;

top=temp;

cout<<del_val<<" is deleted \n";

display();

cout<<"Do u want to delete again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');
}

void display()

node *temp;

if(start==NULL)

cout<<"start-->null\n";

else

cout<<"start-->";

temp=start;

do{

cout<<temp->num<<"-->";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"null\n";

}}

int main()

int ch;

char ask;

menu:

cout<<"enter 1 to push\n";
cout<<"enter 2 to pop\n";

cout<<"enter 3 to display\n";

cout<<"enter 4 to exit\n";

cin>>ch;

if(ch==1)

push();

{goto menu;}

if(ch==2)

pop();

{goto menu;}

if(ch==3)

display();

{goto menu;}

if(ch==4)

cout<<"good bye\n";

else
cout<<"invalid choise\n";

//---26---

#include <iostream>

using namespace std;

static int que[4];

int rear=-1;

int front=-1;

int size=0;

void enqueue(int x)

if(rear<3)

rear++;

que[rear]=x;

size++;

if(front==-1)

front++;

else

cout<<"queue is overflow\n";
}

void dequeue()

int del;

if(size==0)

cout<<"stack is underflow\n";

else

del=que[front];

que[front]=NULL;

front++;

size--;

if(size==0)

rear=front=-1;

cout<<del<<" is deleted\n";

void display()

int i;

cout<<"value\t index\n";
cout<<"===== =====\n";

for(i=0;i<4;i++)

if(que[i]==NULL)

cout<<"[ ]\t "<<i;

else

cout<<"[";

cout<<que[i]<<"]\t "<<i;

if(i==rear)

cout<<"<---rear";

if(i==front)

cout<<"<---front";

cout<<endl;

int main(){

int n,ch;

char ask;

menu:

cout<<"enter 1 to enque\n";
cout<<"enter 2 to dequeue\n";

cout<<"enter 3 to display\n";

cout<<"enter 4 to exit\n";

cin>>ch;

if(ch==1)

add:

cout<<"enter a number\n";

cin>>n;

enqueue(n);

display();

cout<<"do u want to enque again? (y/n)\n";

cin>>ask;

if(ask=='y'||ask=='Y')

{goto add;}

else

{goto menu;}

else if(ch==2)

del:

dequeue();
display();

cout<<"do u want to dequeue again? (y/n)\n";

cin>>ask;

if(ask=='y'||ask=='Y')

{goto del;}

else

{goto menu;}

else if(ch==3)

display();

else if(ch==4)

cout<<"good bye";

else

cout<<"invalid choise\n";

cout<<"pls re-enter again \n";

{goto menu;}

return 0;

}
//---27---

#include <iostream>

using namespace std;

static int que[4];

int rear=-1;

int front=-1;

int size=0;

void enqueue(int x)

if(size<4)

rear++;

if(rear==4)

rear=0;

que[rear]=x;

size++;

if(front==-1)

front++;

else

cout<<"queue is overflow\n";

}
void dequeue()

int del;

if(size==0)

cout<<"stack is underflow\n";

else

if(front==4)

front=0;

del=que[front];

que[front]=NULL;

front++;

size--;

cout<<del<<" is deleted\n";

if(size==0)

rear=-1;

front=-1;

}
void display()

int i;

cout<<"value\t index\n";

cout<<"===== =====\n";

for(i=0;i<4;i++)

if(que[i]==NULL)

cout<<"[ ]\t "<<i;

else

cout<<"[";

cout<<que[i]<<"]\t "<<i;

if(i==rear)

cout<<"<---rear";

if(i==front)

cout<<"<---front";

cout<<endl;

int main(){
int n,ch;

char ask;

menu:

cout<<"enter 1 to enque\n";

cout<<"enter 2 to dequeue\n";

cout<<"enter 3 to display\n";

cout<<"enter 4 to exit\n";

cin>>ch;

if(ch==1)

add:

cout<<"enter a number\n";

cin>>n;

enqueue(n);

display();

cout<<"do u want to enque again? (y/n)\n";

cin>>ask;

if(ask=='y'||ask=='Y')

{goto add;}

else

{goto menu;}

}
else if(ch==2)

del:

dequeue();

display();

cout<<"do u want to dequeue again? (y/n)\n";

cin>>ask;

if(ask=='y'||ask=='Y')

{goto del;}

else

{goto menu;}

else if(ch==3)

display();

else if(ch==4)

cout<<"good bye";

else

cout<<"invalid choise\n";

cout<<"pls re-enter again \n";

{goto menu;}

}
return 0;

#include<iostream>

using namespace std;

struct node{

int num;

node *nxt;

};

void display();

struct node *start=NULL;

node *rear=NULL;

node *front=NULL;

void enqueue()

char ask;

do{

node *temp;

temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;

temp->nxt=NULL;
if(start==NULL)

start= temp;

rear=start;

front=start;

else

rear->nxt =temp;

rear=temp;

display();

cout<<"Do u want to enter again(y/n)\n";

cin>>ask;

}while(ask=='y'||ask=='Y');

void dequeue()

char ask;

int del_val;

do{

if(start==NULL)
cout<<"the queue is empty \n";

else

if(start->nxt==NULL)

del_val=front->num;

delete front;

start=NULL;

front=start;

rear=NULL;

else

start=start->nxt;

del_val=front->num;

delete front;

front=start;

cout<<del_val<<" is deleted \n";

display();

cout<<"Do u want to delete again(y/n)\n";


cin>>ask;

}while(ask=='y'||ask=='Y');

void display()

node *temp;

if(start==NULL)

cout<<"start-->null\n";

else

cout<<"start-->";

temp=start;

do{

cout<<temp->num<<"-->";

temp=temp->nxt;

}while(temp!=NULL);

cout<<"null\n";

}}

int main()

int ch;

char ask;
menu:

cout<<"enter 1 to enqueue\n";

cout<<"enter 2 to dequeue\n";

cout<<"enter 3 to display\n";

cout<<"enter 4 to exit\n";

cin>>ch;

if(ch==1)

enqueue();

{goto menu;}

if(ch==2)

dequeue();

{goto menu;}

if(ch==3)

display();

{goto menu;}

if(ch==4)
cout<<"good bye\n";

else

cout<<"invalid choise\n";

//---28---

//Binary tree

#include<iostream>

using namespace std;

struct node{

int num;

node *left;

node *right;

};

void display();

struct node *root=NULL;

void add(int n)

node *temp;

temp= new node;

temp->num=n;

temp->left= NULL;

temp->right= NULL;
node *target= root;

in1:

if(root== NULL)

root= temp;

else if(n>target->num)

if(target->right== NULL)

target ->right=temp;

else

target= target->right;

if(target!= NULL)

{goto in1;}

else if(n<target->num)

if(target-> left== NULL)

target -> left=temp;

else

target= target->left;
if(target!= NULL)

{goto in1;}

else

cout<<"no duplicate allowed\n";

void search(int key)

int found=0;

node *temp= root;

do{

if( temp->num==key)

found=1;

else

if(key > temp->num)

temp= temp->right;

else

temp=temp->left;

}while( found==0&&temp!= NULL);


if( found==1)

cout<<"the number "<<temp->num<< " is found"<<endl;

else

cout<<"the number "<<temp->num<< " is not found"<<endl;

void preorder(node *temp)

if( temp!= NULL)

cout<<temp->num<< endl;

preorder(temp->right);

preorder(temp->left);

void inorder(node *temp)

if( temp!= NULL)

// cout<<temp->num<< endl;

inorder(temp->right);

cout<<temp->num<< endl;

inorder(temp->left);
}

void post_order(node *temp)

if( temp!= NULL)

post_order(temp->right);

post_order(temp->left);

cout<<temp->num<< endl;

int main()

int x,y,i,ch;

char ask;

in:

cout<<"how many numbers do you want to enter?\n";

cin>>i;

while( i>0)

cout<<"enter a number\n";
cin>>x;

add(x);

i--;

node *tree= root;

choice:

cout<<"enter 1 to display in pre order\n";

cout<<"enter 2 to display in order\n";

cout<<"enter 3 to display in post order\n";

cout<<"enter 4 to binary tree search\n";

cout<<"enter 5 to exit\n";

cin>> ch;

if(ch==1)

cout<<"pre order\n";

preorder(tree);

else if (ch==2)

cout<<"inorder\n";

inorder( tree);
}

else if(ch==3)

cout<<"post_order\n";

post_order( tree);

else if(ch==4)

cout<<"enter the number to be searched\n";

cin>>y;

search(y);

else if(ch==5)

cout<<"Good bye\n";

else

cout<<"Invalid choice\n";

cout<<"do u wanna back to the menu?(y/n)\n";

cin>>ask;

if(ask=='y'||ask=='Y')

{goto choice;}

You might also like