Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Algorithm Lab Manual

The documents contain examples of linear and binary search algorithms implemented in C++. The linear search algorithms search through an unsorted list sequentially to find a target value. The binary search algorithms search through a sorted list by checking the middle element and eliminating half of the remaining elements in each step to find the target value.

Uploaded by

Getaneh Awoke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Algorithm Lab Manual

The documents contain examples of linear and binary search algorithms implemented in C++. The linear search algorithms search through an unsorted list sequentially to find a target value. The binary search algorithms search through a sorted list by checking the middle element and eliminating half of the remaining elements in each step to find the target value.

Uploaded by

Getaneh Awoke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

else

A. Searching
cout<<key<< " is not found\ in the
/*
listn";
Example - 1
}
A linear search code which accept n
int main()
number of integers and a number which is
{
to be searched from the user . It searches
int list[6],i,num,n;
the number in the list and then displays
cout<<" how many numbers do you wa
whether it is in the list or not */
to enter \n";
#include<iostream>
cin>>n;
using namespace std;
for(i=0;i<n;i++)
void linear(int x[],int key,int size)
cin>>list[i];
{
cout<<"enter the number to be
int index=0,found=0;
searched\n";
do{
cin>>num;
if (x[index]==key)
linear(list,num,n);
found=1;
}
else

index++;
===========================
}while(found==0&&index<size);
==========
The output of the above code is the following
if(found==1)

cout<<key<<" is found in the enter six numbers


2
list\n"; 1
5
8
if(x[index]==key)
4
8
found=1;
enter the number to be searched
6
else
6 is not found in the list
index++;

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

if(found==1)

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


/*
Example - 2 else

A linear search code which accept n cout<<key<< " is not found in the

number ofof names and a name which is list\n";

to be searched from the user . It searches }

the name in the list and then displays


int main()
whether it is in the list or not
{
*/
string list[6],name;

#include<iostream> int i;

#include<string> cout<<" enter 6 names\n";

using namespace std; for(i=0;i<6;i++)

cin>>list[i];
void linear(string x[],string key)
cout<<"enter the name to be searched\n";
{
cin>>name;
int index=0,found=0;
linear(list,name);
do{
}
{
===========================
==========
int left=0,found=0,right=5,mid;
The output of the above code is the following
do{
enter six names
abebe mid=(left+right)/2;
kebede
taye if(x[mid]==key)
sara
lily found=1;
dagne
enter the number to be searched else{
lily
lily is found in the list if(key<x[mid])

right=mid-1;

else

left=mid+1;
/*
Example - 3 }

A binary search code which accept n }while(found==0&&left<=right);

number of integers and a number which is if(found==1)

to be searched from the user . It searches cout<<key<<" is found in the list\n";

the number in the list and then displays else

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

*/ }

int main(){
#include<iostream>
int list[6],i,num;
using namespace std;
cout<<" enter six ascendinglly sorted

void binary(int x[],int key) numbers\n";


for(i=0;i<6;i++) void binary(int x[],int key)

cin>>list[i]; {

cout<<"enter the number to be int left=0,found=0,right=5,mid;

searched\n"; do{

cin>>num; mid=(left+right)/2;

binary(list,num); if(x[mid]==key)

} found=1;
===========================
else
==========
The output of the above code is the following
{
===========================
==========
if(key<x[mid])
enter six ascendinglly sorted numbers
2
left=mid+1;
4
5
else
8
11
right=mid-1;
15
enter the number to be searched
}
5
5 is found in the list
}while(found==0&&left<=right);

if(found==1)

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


/*
else
Example - 4
cout<<key<< " is not found\n";
binary search in descending order
}
#include<iostream>
int main()
using namespace std;
{
int list[6],i,num; to be searched from the user . It searches

cout<<" enter 6 numbers in descending the name in the list and then displays

order\n"; whether it is in the list or not

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

cin>>list[i]; #include<iostream>

cout<<"enter the key\n"; #include<string>

cin>>num; using namespace std;

binary(list,num);
void binary(string x[],string key)
}
{
===========================
==========
int left=0,found=0,right=5,mid;
The output of the above code is the following
===========================
do{
==========
enter six numbers in descending order
mid=(left+right)/2;
9
7
if(x[mid]==key)
5
3
found=1;
1
0
else
enter the number to be searched
6
{
6 is not found in the list
if(key<x[mid])

right=mid-1;
/*
else
Example - 5
left=mid+1;
A binary search code which accept n
}
number ofof names and a name which is
}while(found==0&&left<=right);
if(found==1) taye
walelign
cout<<key<<" is found in the list\n";
zufan
else enter the name to be searched

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


kemal is found in the list
}

int main(){

string list[6],name; /*

int i; Example - 6

cout<<" enter sorted 6 names in ascending A binary search code which accept n

order\n"; number of integers.

for(i=0;i<6;i++) and a number which is to be searched

cin>>list[i]; from the user . It searches the number in

cout<<"enter the key\n"; the list and then displays whether it is in

cin>>name; the list or not .

binary(list,name); The program sorts the numbers

} automatically while the user enters each


=========================== number .
==========
*/
The output of the above code is the following
===========================
#include<iostream>
==========
enter six names in ascending order using namespace std;
abebe void binary(int x[],int key)
kebede
{
sisay
int left=0,found=0,right=5,mid; for(i=0;i<6;i++)

do{ {

mid=(left+right)/2; cout<<"enter a numbers\n";

if(x[mid]==key) cin>>num;

found=1; y[i]=num;

else if(i==0)

{ x[i]=num;

if(key<x[mid]) for(j=i-1;j>=0;j--)

right=mid-1; {

else if(num<x[j])

left=mid+1; {

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

}while(found==0&&left<=right); x[j]=num;

if(found==1) }

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

else {

cout<<key<< " is not found\n"; x[j+1]=num;

} break;

}
int main()
}
{
}
int num, i,j,x[6];
int k;
int y[6];
3
cout<<"enter a number to be searched\n"; 1
0
cin>>k; The numbers before sorted
9,7,5,3,1,0
cout<<"the numbers before sorted\n"; The numbers after sorted
0,1,3,5,7,9
for(i=0;i<6;i++)
enter the number to be searched
cout<<y[i]<<" , "; 5

cout<<endl; 5 is found in the list

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

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

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

cout<<endl;

binary(x,k);
//---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*/
The output of the above code is the

following #include<iostream>

========================= using namespace std;

============ void binary(int x[],int key)

enter six numbers {

9 int left=0,found=0,right=5,mid;
7
int index;
5
do{ {

mid=(left+right)/2; int num, i,j,x[6];

if(x[mid]==key) int y[6];

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

found=1; {

//index=mid; add:

} cout<<"enter a numbers\n";

else cin>>num;

{ y[i]=num;

if(key<x[mid]) if(i==0)

right=mid-1; x[i]=num;

else else

left=mid+1; {

} if(num<x[i-1])

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

if(found==1) cout<<" please re-enter a number which is

greater than. "<<x[i-1]<<endl;


cout<<key<<" is found in the list at index
{goto add;}
of "<<mid<<"\n";
}
else
else
cout<<key<< " is not found\n";
x[i]=num;
}
}
int main()
7
}
1
int k;

cout<<"enter a number to be searched\n"; please re-enter a number which is greater

cin>>k; than 7
9

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

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

cout<<endl;
enter the number to be searched
binary(x,k);
6
}
6 is not found in the list

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

=======

The output of the above code is the

following
B. Sorting
==============================
//---8---
=======
*/a bubble sort code that accepts unsorted
enter six numbers in ascending order
3
numbers from the keyboard and it sorts

2
the numbers in ascending order then it

please re-enter a number which is greater displays them before and after sorted*/

than 3 #include <iostream>

5 using namespace std;


void bubble(int x[]) for(i=0;i<6;i++)

{ cin>>y[i];

int i,j,temp; cout<<"the numbers before sorted\n";

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

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

for(j=5;j>i;j--) cout<<endl;

{ bubble(y);

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

{ ==============================

temp=x[j]; =======

x[j]=x[j-1]; The output of the above code is the

x[j-1]=temp; following

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

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

cout<<endl;} enter six unsorted numbers

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


7
for(i=0;i<6;i++)
5
cout<<x[i]<<" , "; 0
1
} 3
The numbers before sorted
int main(){ 9,7,5,0,1,3
The numbers after sorted
int y[6],i; 0,1,3,5,7,9

cout<<"enter 6 unsorted numbers\n"; //---9---


*/a bubble sort code that accepts unsorted for(i=0;i<6;i++)

numbers from the keyboard and it sorts cout<<x[i]<<" , ";

the numbers in descending order then it }

displays them before and after sorted*/ int main()

#include <iostream> {

using namespace std; int y[6],i;

void bubble(int x[]) cout<<"enter 6 unsorted numbers\n";

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

int i,j,temp; cin>>y[i];

for(i=0;i<6;i++) cout<<"the numbers before sorted\n";

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

for(j=5;j>i;j--) cout<<y[i]<<" , ";

{ cout<<endl;

if(x[j]<x[j-1]) bubble(y);

{ }

temp=x[j]; ==============================

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

x[j-1]=temp; The output of the above code is the

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

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

cout<<endl;} =======

cout<<"the numbers after sorted\n"; enter six unsorted numbers


4
7
{
5
0
for(j=5;j>i;j--)
1
3
{
The numbers before sorted
4,7,5,0,1,3
if(x[j]>x[j-1])
The numbers after sorted
7,5,4,3,1,0
{

temp=x[j];

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

*/a bubble sort code that accepts unsorted x[j-1]=temp;

n numbers of names from the keyboard }}

and it sorts the names in descending for(int k=0;k<6;k++)

order then it displays them before and cout<<x[k]<<" , ";

after sorted*/ cout<<endl;

}
//in descending order
cout<<"the names after sorted in
#include <iostream>
descending order\n";
#include<string>

using namespace std; for(i=0;i<6;i++)

void bubble(string x[]) cout<<x[i]<<" , ";

{ }

string temp; int main()

int i,j; {

string x[6];
for(i=0;i<6;i++)
int i;
chala
cout<<"enter 6 unsorted names\n"; martha
jemal
for(i=0;i<6;i++) tsion
masresha
cin>>x[i];
The names after sorted
zelalem
cout<<"the names before sorted\n"; tsion
masresha
for(i=0;i<6;i++) jemal
chala
cout<<x[i]<<" , "; belay
abebe
cout<<endl;

bubble(x);

============================== //---11---

======= */a insertion sort code that accepts

The output of the above code is the unsorted numbers from the keyboard and

following it sorts the numbers in ascending order

============================== then it displays them before and after

======= sorted*/

enter six unsorted names


abebe
#include <iostream>
belay
zelalem
using namespace std;
chala
martha
void insertion(int x[])
jemal
tsion
{
masresha
The names before sorted
int i,j,temp;
abebe
belay
zelalem
for(i=1;i<6;i++)
{ for(i=0;i<6;i++)

temp=x[i]; cin>>y[i];

for(j=i;j>0&&temp<x[j-1];j--) cout<<"the numbers before sorted\n";

{
for(i=0;i<6;i++)
x[j]=x[j-1];
cout<<y[i]<<" , ";
x[j-1]=temp;
cout<<endl;

} insertion(y);

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

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

cout<<endl; ==============================

} =======

cout<<"the numbers after using insertion The output of the above code is the

sort\n"; following

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

} enter six unsorted numbers

int main() 4
7
{
5
int y[6]; 0
1
int i; 3

cout<<"enter 6 unsorted numbers\n"; The numbers before sorted


4,7,5,0,1,3
{

The numbers after using insertion sort


temp=x[i];

7,5,4,3,1,0
for(j=i;j>0&&temp>x[j-1];j--)

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

x[j-1]=temp;

//---12--- for(int k=0;k<6;k++)

*/a insertion sort code that accepts cout<<x[k]<<" , ";

unsorted n numbers of names from the cout<<endl;

keyboard and it sorts the names in }

descending order then it displays them cout<<"the names after sorted using

before and after sorted*/ insertion sort\n";

#include <iostream>
for(i=0;i<6;i++)
#include<string>
cout<<x[i]<<" , ";
using namespace std;
}
void insertion(string x[])
int main()
{
{
int i,j;
string y[6];
string temp;
int i;

for(i=1;i<6;i++) cout<<"enter 6 unsorted names\n";


jemal
for(i=0;i<6;i++) tsion
masresha
cin>>y[i];
The names after sorted using insertion sort
cout<<"the names before sorted\n"; zelalem
tsion
masresha
for(i=0;i<6;i++) jemal
chala
cout<<y[i]<<" , "; belay
abebe
cout<<endl;

insertion(y);

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

=======
//---13---
The output of the above code is the

following */a insertion sort code that accepts

============================== unsorted numbers from the keyboard and

======= it sorts the numbers in descending order

enter six unsorted names then it displays them before and after
abebe
belay
sorted*/
zelalem
chala
martha
#include <iostream>
jemal
tsion
using namespace std;
masresha
The names before sorted
void insertion(int x[])
abebe
belay
{
zelalem
chala
int i,j,temp;
martha
for(i=1;i<6;i++) for(i=0;i<6;i++)

{ cin>>y[i];

temp=x[i];
cout<<"the numbers before sorted\n";
for(j=i;j>0&&temp>x[j-1];j--)

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

x[j]=x[j-1]; cout<<y[i]<<" , ";

x[j-1]=temp; cout<<endl;

} insertion(y);

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

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

cout<<endl;

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

cout<<"the numbers after sorted using =======

insertion sort in descending order\n"; The output of the above code is the

following
for(i=0;i<6;i++)
==============================
cout<<x[i]<<" , ";
=======
}

int main() enter six unsorted numbers

{ 4
7
int y[6],i;
5
cout<<"enter 6 unsorted numbers\n"; 0
1
3
{

The numbers before sorted


int i,j,small;

4,7,5,0,1,3
string temp;

The numbers after using insertion sort in descending


for(i=0;i<6;i++)
order
{
7,5,4,3,1,0
small=i;

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

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

small=j;

temp=x[i];
//---14-----
x[i]=x[small];
*/a selection sort code that accepts
x[small]=temp;
unsorted numbers from the keyboard and
for(int k=0;k<6;k++)
it sorts the numbers in ascending order
cout<<x[k]<<" , ";
then it displays them before and after
cout<<endl;
sorted*/
}

#include <iostream> cout<<"the names after sorted using

#include<string> selection sort in ascending order\n";

using namespace std; for(i=0;i<6;i++)

void selection(string x[]) cout<<x[i]<<" , ";


} enter six unsorted numbers

9
int main() 7

{ 5
0
string y[6]; 1
3
int i;
The numbers before sorted
cout<<"enter 6 unsorted names\n";
9,7,5,0,1,3
for(i=0;i<6;i++)
The numbers after sorted using selection sort in
cin>>y[i]; ascending order

0,1,3,5,7,9
cout<<"the names before sorted\n";

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

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

cout<<endl;
//---15---

selection(y); #include <iostream>

} #include<string>

using namespace std;

============================== int main()

======= {

The output of the above code is the string num;

following int i,j;

============================== string x[6];

======= string y[6];


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

while(i<6) cout<<x[k]<<" ";

{ cout<<endl;

cout<<"enter a name\n"; }

cin>>num; i++;

y[i]=num; }

if(i==0) cout<<"the names before sorted\n";

x[i]=num; for(i=0;i<6;i++)

j=i-1; cout<<y[i]<<" , ";

while(j>=0) cout<<endl;

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

if(x[j]<num) for(i=0;i<6;i++)

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

x[j+1]=num; }

break; //---16---

} #include <iostream>

else #include<string>

{ using namespace std;

x[j+1]=x[j]; void selection(char x[])

x[j]=num; {

} int i,j,small;

char temp;
j--;
for(i=0;i<6;i++) char y[6];

{ int i;

small=i; cout<<"enter 6 unsorted character s\n";

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

{ cin>>y[i];

if(x[j]<x[small]) cout<<"the characters before sorted\n";

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

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

temp=x[i]; cout<<endl;

x[i]=x[small]; selection(y);

x[small]=temp; }

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

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

cout<<endl;

} C. Single linked
cout<<"the letter after sorted using
list
selection sort\n";
//---17---
for(i=0;i<6;i++)
// add at the end
cout<<x[i]<<" , ";
#include<iostream>
}
using namespace std;
int main()
struct node{
{
int num;
node *nxt; }

}; count++;

int count=0; cout<<count<<"nodes has the list\n";

void display(); display();

struct node *start=NULL; cout<<"Do u want to enter again(y/n)\n";

cin>>ask;
void add_end()
}while(ask=='y'||ask=='Y');
{
}
char ask;
void display()
do{
{
node *temp;
node *temp;
temp =new node;
if(start==NULL)
cout<<"Enter a number\n";
cout<<"start-->null\n";
cin>>temp->num;
else
temp->nxt=NULL;
{
if(start==NULL)
cout<<"start-->";
start= temp;
temp=start;
else
do{
{
cout<<temp->num<<"-->";
node *target=start;
temp=temp->nxt;
while(target->nxt!= NULL)
}while(temp!=NULL);
target=target->nxt;
cout<<"null\n";
target->nxt=temp;
}} int i;

int main() do{

{ node *temp1;

add_end(); node *temp;

} temp =new node;

cout<<"Enter a number\n";

cin>>temp->num;
//---18---
// add in the middle
if(start==NULL)

#include<iostream> {

using namespace std; temp->nxt=NULL;

struct node{ start= temp;

int num; count=1;

node *nxt; cout<<count<<"nodes has the list\n";

}; display();

int count=0; }

void display(); else{

struct node *start=NULL; cout<<"After which node do u want to

insert it\n";
void addmiddle()
cin>>place;
{
if(place>count)
char ask;
do{
int place;
cout<<"there is only "<<count<<" nodes .
please enter again\n"; else

cin>>place; {

}while(place>count); cout<<"start-->";

temp=start;
temp1= start;
do{

for(i=1;i!=place;i++) cout<<temp->num<<"-->";

temp1=temp1->nxt; temp=temp->nxt;

}while(temp!=NULL);
temp->nxt=temp1->nxt;
cout<<"null\n";
temp1->nxt= temp;
}}
count++;

cout<<count<<"nodes has the list\n"; int main()

display(); {

} addmiddle();

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

cin>>ask;

}while(ask=='y'||ask=='Y');
//---19---
} //delete anywhere

void display() #include<iostream>

{ using namespace std;

node *temp; struct node{

if(start==NULL) int num;

cout<<"start-->null\n"; node *nxt;


}; it?\n";

int count=0; cin>>place;

void display(); if(place>count)

struct node *start=NULL; do{

cout<<"there is only "<<count<<" nodes .


void deleteanywhere()
please enter again\n";
{
cin>>place;
char ask;
}while(place>count);
int place;

int i; target= start;

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

do{ {

node *temp1; del=target->num;

node *target; start=target->nxt;

delete target;
if(start==NULL)
cout<<del<<" is deleted succefully\n";
{
count--;

cout<<"The list has "<<count<<" nodes }

has the list\n"; else

display(); {

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

else{ {

cout<<" which node do u want to delete temp1=target;


target=target->nxt; cin>>temp->num;

} temp->nxt=NULL;

temp1->nxt=target->nxt; if(start==NULL)

del=target->num; start= temp;

delete target; else

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

count--; node *target=start;

} while(target->nxt!= NULL)

cout<<count<<"nodes has the list\n"; target=target->nxt;

display(); target->nxt=temp;

} }

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

cin>>ask; cout<<count<<"nodes has the list\n";

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

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

void add_end() cin>>ask;

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

char ask; }

do{
void display()
node *temp;
{
temp =new node;
node *temp;
cout<<"Enter a number\n";
if(start==NULL)
cout<<"start-->null\n"; node *nxt;

else };

{ int count=0;

cout<<"start-->"; void display();

temp=start; struct node *start=NULL;

do{
void deleteEnd()
cout<<temp->num<<"-->";
{
temp=temp->nxt;
int del;
}while(temp!=NULL);
char ask;
cout<<"null\n";
do{
}}
node *temp;
int main()
if(start==NULL)
{
cout<<"the list is empty\n";
add_end();
else
deleteanywhere();
{
}
temp=start;

//---20 ---- if(temp->nxt==NULL)

//delete end {

#include<iostream> del=temp->num;

using namespace std; start=NULL;

struct node{ delete temp;

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


} void add_start()

else {

{ char ask;

node *target; do{

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

{ temp =new node;

target=temp; cout<<"Enter a number\n";

temp=temp->nxt; cin>>temp->num;

} if(start==NULL)

del= temp->num; {

target->nxt= NULL; start= temp;

delete temp; temp->nxt=NULL;

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

}
else
}
{
count--;
temp->nxt=start;
cout<<count<<" : nodes has the list\n";
start= temp;
display();
}
cout<<"Do u want to delete again(y/n)\n";
count++;
cin>>ask;
cout<<count<<" : nodes has the list\n";
}while(ask=='y'||ask=='Y');
display();
}
cout<<"Do u want to enter again(y/n)\n";
cin>>ask; }

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

void display() //---21---


//delete start
{
#include<iostream>
node *temp;
using namespace std;
if(start==NULL)
struct node{
cout<<"start-->null\n";
int num;
else
node *nxt;
{
};
cout<<"start-->";
int count=0;
temp=start;
void display();
do{
struct node *start=NULL;
cout<<temp->num<<"-->";

temp=temp->nxt; void deleteStart()

}while(temp!=NULL); {

cout<<"null\n"; int del;

}} char ask;

int main () do{

{ node *temp;

add_start(); if(start==NULL)

deleteEnd(); cout<<"the list is empty\n";


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

{ }

temp=start; void add_start()

if(temp->nxt==NULL) {

{ char ask;

del=temp->num; do{

start=NULL; node *temp;

delete temp; temp =new node;

cout<<del<<" is deleted\n"; cout<<"Enter a number\n";

} cin>>temp->num;

else if(start==NULL)

{ {

start=temp->nxt; start= temp;

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

delete temp; }

cout<<del<<" is deleted\n";
else
}}
{
count--;
temp->nxt=start;
cout<<count<<" : nodes has the list\n";
start= temp;
display();
}

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

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


display(); add_start();

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

cin>>ask; }

}while(ask=='y'||ask=='Y'); //--22---

} //sort

#include<iostream>
void display()
using namespace std;
{
struct node{
node *temp;
int num;
if(start==NULL)
node *nxt;
cout<<"start-->null\n";
};
else
int count=0;
{
void display();
cout<<"start-->";
struct node *start=NULL;
temp=start;

do{ void sort()

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

temp=temp->nxt; int temp,ch;

}while(temp!=NULL); node *t1,*t2,*t3,*t4;

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

}} display();

int main() int i;

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

for(t3=start,t2=t3- }

>nxt;t2!=NULL;t3=t2,t2=t2->nxt) }

{ cout<<"The numbers after sorted in

if(t3->num>t2->num) ascending order\n";

{ display();

if(start==t3) }

{ void add_start()

start=t2; {

t3->nxt=t2->nxt; char ask;

t2->nxt= t3; do{

t1=start; node *temp;

} temp =new node;

else cout<<"Enter a number\n";

{ cin>>temp->num;

t4=start; if(start==NULL)

while(t4->nxt!=t3) {

t4=t4->nxt; start= temp;

t3->nxt=t2->nxt; temp->nxt=NULL;

t2->nxt=t4->nxt; }

t4->nxt=t2;
else
}
{
temp->nxt=start; }while(temp!=NULL);

start= temp; cout<<"null\n";

} }}

count++; int main()

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

display(); add_start();

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

cin>>ask; }

}while(ask=='y'||ask=='Y');
//---23---
}
/* A single linked list code which

void display() performs all the following tasks in one

{ program

node *temp; 1. add nodes at the end

if(start==NULL) 2. add nodes at the start

cout<<"start-->null\n"; 3. delete nodes at the end

else 4. delete nodes at the start

{ 5. add nodes somewhere in the middle

cout<<"start-->"; 6.delete nodes somewhere in the middle

temp=start; 7. sort the lists

do{ 8. display the lists*/

cout<<temp->num<<"-->";
#include<iostream>
temp=temp->nxt;
using namespace std; while(target->nxt!= NULL)

struct node{ target=target->nxt;

int num; target->nxt=temp;

node *nxt; }

}; count++;

int count=0; cout<<count<<"nodes has the list\n";

void display(); display();

struct node *start=NULL; cout<<"Do u want to enter again(y/n)\n";

void add_end() cin>>ask;

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

char ask; }

do{
void addmiddle()
node *temp;
{
temp =new node;
char ask;
cout<<"Enter a number\n";
int place;
cin>>temp->num;
int i;
temp->nxt=NULL;

if(start==NULL) do{

start= temp; node *temp1;

else node *temp;

{ temp =new node;

node *target=start; cout<<"Enter a number\n";


cin>>temp->num; temp1=temp1->nxt;

if(start==NULL)
temp->nxt=temp1->nxt;
{
temp1->nxt= temp;
temp->nxt=NULL;
count++;
start= temp;
cout<<count<<"nodes has the list\n";
count=1;
display();
cout<<count<<"nodes has the list\n";
}
display();
cout<<"Do u want to enter again(y/n)\n";
}
cin>>ask;
else{
}while(ask=='y'||ask=='Y');
cout<<"After which node do u want to
}
insert it\n";

cin>>place; void deleteanywhere()

if(place>count) {

do{ char ask;

cout<<"there is only "<<count<<" nodes . int place;

please enter again\n"; int i;

cin>>place; int del;

}while(place>count); do{

node *temp1;
temp1= start;
node *target;

for(i=1;i!=place;i++)
if(start==NULL)
{ cout<<del<<" is deleted succefully\n";

count--;
cout<<"The list has "<<count<<" nodes
}
has the list\n";
else
display();
{
}
for(i=1;i!=place;i++)
else{
{
cout<<" which node do u want to delete
temp1=target;
it?\n";
target=target->nxt;
cin>>place;
}
if(place>count)
temp1->nxt=target->nxt;
do{
del=target->num;
cout<<"there is only "<<count<<" nodes .
delete target;
please enter again\n";
cout<<del<<" is deleted succefully\n";
cin>>place;
count--;
}while(place>count);
}

target= start; cout<<count<<"nodes has the list\n";

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

{ }

del=target->num; cout<<"Do u want to delete again(y/n)\n";

start=target->nxt; cin>>ask;

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


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

void add_start() cin>>ask;

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

char ask; }

do{ void deleteEnd()

node *temp; {

temp =new node; int del;

cout<<"Enter a number\n"; char ask;

cin>>temp->num; do{

if(start==NULL) node *temp;

{ if(start==NULL)

start= temp; cout<<"the list is empty\n";

temp->nxt=NULL; else

} {

temp=start;
else
if(temp->nxt==NULL)
{
{
temp->nxt=start;
del=temp->num;
start= temp;
start=NULL;
}
delete temp;
count++;
cout<<del<<" is deleted\n";
cout<<count<<" : nodes has the list\n";
}
display();
else {

{ int del;

node *target; char ask;

while(temp->nxt!= NULL) do{

{ node *temp;

target=temp; if(start==NULL)

temp=temp->nxt; cout<<"the list is empty\n";

} else

del= temp->num; {

target->nxt= NULL; temp=start;

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

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

} del=temp->num;

} start=NULL;

count--; delete temp;

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

display(); }

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

cin>>ask; {

}while(ask=='y'||ask=='Y'); start=temp->nxt;

} del= temp->num;

delete temp;
void deleteStart()
cout<<del<<" is deleted\n"; {

}} if(start==t3)

count--; {

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

display(); t3->nxt=t2->nxt;

t2->nxt= t3;
cout<<"Do u want to delete again(y/n)\n";
t1=start;
cin>>ask;
}
}while(ask=='y'||ask=='Y');
else
}
{
void sort()
t4=start;
{
while(t4->nxt!=t3)
int temp,ch;
t4=t4->nxt;
node *t1,*t2,*t3,*t4;
t3->nxt=t2->nxt;
cout<<"The numbers before sorted\n";
t2->nxt=t4->nxt;
display();
t4->nxt=t2;
int i;
}
for(t1=start;t1!=NULL ;t1=t1->nxt)
}
{
}
for(t3=start,t2=t3-
}
>nxt;t2!=NULL;t3=t2,t2=t2->nxt)
cout<<"The numbers after sorted in
{
ascending order\n";
if(t3->num>t2->num)
display(); cout<<"enter 1 to add at end\n";

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

void display() cout<<"enter 3 to delete at the end\n";

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

node *temp; cout<<"enter 5 to display\n";

if(start==NULL) cout<<"enter 6 to add in the middle\n";

cout<<"start-->null\n"; cout<<"enter 7 to delete in the middle\n";

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

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

cout<<"start-->"; cin>>ch;

temp=start; if(ch==1)

do{ {

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

temp=temp->nxt; {goto menu;}

}while(temp!=NULL); }

cout<<"null\n"; else if(ch==2)

}} {

int main() add_start();

{ {goto menu;}

int ch; }

char ask; else if(ch==3)

menu: {
deleteEnd(); }

{goto menu;} else if(ch==8)

} {

else if (ch==4) sort();

{ {goto menu;}

deleteStart(); }

{goto menu;} else if(ch==9)

} cout<<"good bye\n";

else if(ch==5) else

{ cout<<"invalid choise\n";

display(); }

{goto menu;}

else if(ch==6)
D. double linked
{
list
//---24---
addmiddle();
#include<iostream>
{goto menu;}
using namespace std;
}
struct node{
else if(ch==7)
int num;
{
node *nxt;
deleteanywhere();
node *prv;
{goto menu;}
};
struct node *start=NULL; while(target->nxt!= NULL)

node *current; target=target->nxt;

void display(); target->nxt=temp;

temp->prv=target;
void addend()
}
{
display();
char ask;
cout<<"Do u want to enter again(y/n)\n";
do{
cin>>ask;
node *temp;
}while(ask=='y'||ask=='Y');
temp =new node;
}
cout<<"Enter a number\n";

cin>>temp->num; void deleteend()

temp->nxt=NULL; {

if(start==NULL) char ask;

{ int del;

start= temp; do{

current=temp; node *temp;

current->nxt=NULL; node *target=current;

current->prv=NULL;
if(start==NULL)
}
cout<<"the list is empty\n";
else

{ else if(target->nxt==NULL&& current-

node *target=current; >prv==NULL)


{ }

del=target->num; del= target->num;

start=NULL; temp->nxt=NULL;

current->nxt=NULL; delete target;

current->prv=NULL; cout<<del<<" is deleted succsefully\n";

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

} display();

else if(target->nxt==NULL && target- cout<<"Do u want to delete again(y/n)\n";

>prv!=NULL) cin>>ask;

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

del=target->num; }

current=current->prv;
void deletestart()
current->nxt=NULL;
{
delete target;
char ask;
cout<<del<<" is deleted successfully\n";
int del;
}
do{
else
node *temp;
{
node *target=current;
while(target->nxt!= NULL)

{ if(start==NULL)

temp=target; cout<<"the list is empty\n";

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

{ }

del=target->num; del= target->num;

start=NULL; temp->prv=NULL;

current->nxt=NULL; delete target;

current->prv=NULL; cout<<del<<" is deleted succsefully\n";

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

} display();

else if(target->prv==NULL && target- cout<<"Do u want to delete again(y/n)\n";

>nxt!=NULL) cin>>ask;

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

del=target->num; }

current=current->nxt;
void addstart()
current->prv=NULL;
{
delete target;
char ask;
cout<<del<<" is deleted successfully\n";
do{
}
node *temp;
else
temp =new node;
{
cout<<"Enter a number\n";
while(target->prv!= NULL)
cin>>temp->num;
{
temp->prv=NULL;
temp=target;
if(start==NULL)
{ node *temp;

start= temp; if(start==NULL)

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

current->nxt=NULL; else

current->prv=NULL; {

} cout<<"NULL<---";

else temp=current;

{ while(temp->prv!=NULL)

node *target=current; temp=temp->prv;

while(target->prv!= NULL) do{

target=target->prv; if(temp==current)

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

temp->nxt=target; else

} {

if(temp->nxt==NULL)
display();
cout<<temp->num<<"--->";
cout<<"Do u want to enter again(y/n)\n";
else
cin>>ask;
cout<<temp->num<<"<==>";
}while(ask=='y'||ask=='Y');
}
}
temp=temp->nxt;

void display() }while(temp!=NULL);

{ cout<<"NULL\n";
}} else if(ch==3)

int main() {

{ deleteend();

int ch; {goto menu;}

menu: }

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


else if(ch==4)
cout<<"enter 2 to add start\n";
{
cout<<"Enter 3 to delete at the end\n";
deletestart();
cout<<"Enter 4 to delete at the start\n";
{goto menu;}
cout<<"enter 5 to display\n";
}
cout<<" enter 6 to exit\n";
else if(ch==5)
cin>>ch;
{
if(ch==1)
display();
{
{goto menu;}
addend();
}
{goto menu;}
else if(ch==6)
}
cout<<"good bye\n";
else if(ch==2)
else
{
cout<<"invalid choice\n";
addstart();
}
{goto menu;}

} #include <iostream>
using namespace std; top--;

static int stack[4]; cout<<del<<" is deleted\n";

int top=-1; }

void push(int x) }

{
void display()
if(top<3)
{
{
int i;
top ++;
cout<<"value\t index\n";
stack[top]=x;
cout<<"===== =====\n";
}
for(i=0;i<4;i++)
else
{
cout<<"stack is overflow\n";
if(stack[i]==NULL)
}
cout<<"[ ]\t "<<i;

void pop () else

{ {

int del; cout<<"[";

if(top==-1) cout<<stack[i]<<"]\t "<<i;

cout<<"stack is underflow\n"; if(i==top)

else cout<<"<---top";

{ }

del=stack[top]; cout<<endl;

stack[top]==NULL; }
} {goto menu;}

int main(){ }

int n,ch;
else if(ch==2)
char ask;
{
menu:
del:
cout<<"enter 1 to push\n";
pop();
cout<<"enter 2 to pop\n";
display();
cout<<"enter 3 to display\n";
cout<<"do u want to pop again? (y/n)\n";
cout<<"enter 4 to exit\n";
cin>>ask;
cin>>ch;
if(ask=='y'||ask=='Y')
if(ch==1)
{goto del;}
{
else
add:
{goto menu;}
cout<<"enter a number\n";
}
cin>>n;
else if(ch==3)
push(n);
display();
display();
else if(ch==4)
cout<<"do u want to push again? (y/n)\n";
cout<<"good bye";
cin>>ask;
else
if(ask=='y'||ask=='Y')
{
{goto add;}
cout<<"invalid choise\n";
else
cout<<"pls re-enter again \n";
{goto menu;} cin>>temp->num;

} temp->nxt=NULL;

return 0; if(start==NULL)

} {

start= temp;

//---25--- top=start;

#include<iostream> }

using namespace std; else

struct node{ {

int num; top->nxt =temp;

node *nxt; top=temp;

}; }

void display(); display();

struct node *start=NULL; cout<<"Do u want to enter again(y/n)\n";

node *top=NULL; cin>>ask;

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

{ }

char ask;
void pop()
do{
{
node *temp;
char ask;
temp =new node;
int del_val;
cout<<"Enter a number\n";
do{
if(start==NULL) display();

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

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

{ cin>>ask;

if(start->nxt==NULL) }while(ask=='y'||ask=='Y');

{ }

del_val=top->num;
void display()
delete top;
{
start=NULL;
node *temp;
top=start;
if(start==NULL)
}
cout<<"start-->null\n";
else
else
{
{
node *temp=start;
cout<<"start-->";
while(temp->nxt!=top)
temp=start;
temp=temp->nxt;
do{
del_val=top->num;
cout<<temp->num<<"-->";
temp->nxt =NULL;
temp=temp->nxt;
delete top;
}while(temp!=NULL);
top=temp;
cout<<"null\n";
}
}}
cout<<del_val<<" is deleted \n";
int main()
{ {goto menu;}

int ch; }

char ask;
if(ch==4)
menu:
cout<<"good bye\n";
cout<<"enter 1 to push\n";
else
cout<<"enter 2 to pop\n";
cout<<"invalid choise\n";
cout<<"enter 3 to display\n";
}
cout<<"enter 4 to exit\n";

cin>>ch;
//---26---
if(ch==1)
#include <iostream>
{
using namespace std;
push();
static int que[4];
{goto menu;}
int rear=-1;
}
int front=-1;
if(ch==2)
int size=0;
{
void enqueue(int x)
pop();
{
{goto menu;}
if(rear<3)
}
{
if(ch==3)
rear++;
{
que[rear]=x;
display();
size++;
if(front==-1)
void display()
front++;
{
}
int i;
else
cout<<"value\t index\n";
cout<<"queue is overflow\n";
cout<<"===== =====\n";
}
for(i=0;i<4;i++)

void dequeue() {

{ if(que[i]==NULL)

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

if(size==0) else

cout<<"stack is underflow\n"; {

else cout<<"[";

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

del=que[front]; if(i==rear)

que[front]=NULL; cout<<"<---rear";

front++; if(i==front)

size--; cout<<"<---front";

if(size==0) }

rear=front=-1; cout<<endl;

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

} }

} int main(){
int n,ch; }

char ask;
else if(ch==2)
menu:
{
cout<<"enter 1 to enque\n";
del:
cout<<"enter 2 to dequeue\n";
dequeue();
cout<<"enter 3 to display\n";
display();
cout<<"enter 4 to exit\n";
cout<<"do u want to dequeue again?
cin>>ch;
(y/n)\n";
if(ch==1)
cin>>ask;
{
if(ask=='y'||ask=='Y')
add:
{goto del;}
cout<<"enter a number\n";
else
cin>>n;
{goto menu;}
enqueue(n);
}
display();
else if(ch==3)
cout<<"do u want to enque again?
display();
(y/n)\n";
else if(ch==4)
cin>>ask;
cout<<"good bye";
if(ask=='y'||ask=='Y')
else
{goto add;}
{
else
cout<<"invalid choise\n";
{goto menu;}
cout<<"pls re-enter again \n";
{goto menu;} if(front==-1)

} front++;

return 0; }

} else

cout<<"queue is overflow\n";

//---27---
void dequeue()
#include <iostream>
{
using namespace std;
int del;
static int que[4];
if(size==0)
int rear=-1;
cout<<"stack is underflow\n";
int front=-1;
else
int size=0;
{
void enqueue(int x)
if(front==4)
{
front=0;
if(size<4)
del=que[front];
{
que[front]=NULL;
rear++;
front++;
if(rear==4)
size--;
rear=0;
cout<<del<<" is deleted\n";
que[rear]=x;
}
size++;
if(size==0)
{ cout<<"<---front";

rear=-1; }

front=-1; cout<<endl;

} }

}
}
int main(){

void display() int n,ch;

{ char ask;

int i; menu:

cout<<"value\t index\n"; cout<<"enter 1 to enque\n";

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

for(i=0;i<4;i++) cout<<"enter 3 to display\n";

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

if(que[i]==NULL) cin>>ch;

cout<<"[ ]\t "<<i; if(ch==1)

else {

{ add:

cout<<"["; cout<<"enter a number\n";

cout<<que[i]<<"]\t "<<i; cin>>n;

if(i==rear) enqueue(n);

cout<<"<---rear"; display();

if(i==front) cout<<"do u want to enque again?


(y/n)\n"; else if(ch==4)

cin>>ask; cout<<"good bye";

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

{goto add;} {

else cout<<"invalid choise\n";

{goto menu;} cout<<"pls re-enter again \n";

} {goto menu;}

}
else if(ch==2)
return 0;
{
}
del:

dequeue();

display(); #include<iostream>

cout<<"do u want to dequeue again? using namespace std;

(y/n)\n"; struct node{

cin>>ask; int num;

if(ask=='y'||ask=='Y') node *nxt;

{goto del;} };

else void display();

{goto menu;} struct node *start=NULL;

} node *rear=NULL;

else if(ch==3) node *front=NULL;

display(); void enqueue()


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

char ask; }

do{
void dequeue()
node *temp;
{
temp =new node;
char ask;
cout<<"Enter a number\n";
int del_val;
cin>>temp->num;
do{
temp->nxt=NULL;
if(start==NULL)
if(start==NULL)
cout<<"the queue is empty \n";
{
else
start= temp;
{
rear=start;
if(start->nxt==NULL)
front=start;
{
}
del_val=front->num;
else
delete front;
{
start=NULL;
rear->nxt =temp;
front=start;
rear=temp;
rear=NULL;
}
}
display();
else
cout<<"Do u want to enter again(y/n)\n";
{
cin>>ask;
start=start->nxt;
del_val=front->num; temp=temp->nxt;

delete front; }while(temp!=NULL);

front=start; cout<<"null\n";

} }}

cout<<del_val<<" is deleted \n"; int main()

display(); {

} int ch;

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

cin>>ask; menu:

}while(ask=='y'||ask=='Y'); cout<<"enter 1 to enqueue\n";

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

cout<<"enter 3 to display\n";
void display()
cout<<"enter 4 to exit\n";
{
cin>>ch;
node *temp;
if(ch==1)
if(start==NULL)
{
cout<<"start-->null\n";
enqueue();
else
{goto menu;}
{
}
cout<<"start-->";
if(ch==2)
temp=start;
{
do{
dequeue();
cout<<temp->num<<"-->";
{goto menu;} void display();

} struct node *root=NULL;

if(ch==3) void add(int n)

{ {

display(); node *temp;

{goto menu;} temp= new node;

} temp->num=n;

temp->left= NULL;
if(ch==4)
temp->right= NULL;
cout<<"good bye\n";
node *target= root;
else
in1:
cout<<"invalid choise\n";
if(root== NULL)
}
root= temp;

//---28---
else if(n>target->num)
//Binary tree
{
#include<iostream>
if(target->right== NULL)
using namespace std;
target ->right=temp;
struct node{
else
int num;
{
node *left;
target= target->right;
node *right;
if(target!= NULL)
};
{goto in1;}
} found=1;

} else

else if(n<target->num) {

{ if(key > temp->num)

if(target-> left== NULL) temp= temp->right;

target -> left=temp; else

else temp=temp->left;

{ }

target= target->left; }while( found==0&&temp!= NULL);

if(target!= NULL) if( found==1)

{goto in1;} cout<<"the number "<<temp->num<< "

} is found"<<endl;

} else

else cout<<"the number "<<temp->num<< "

cout<<"no duplicate allowed\n"; is not found"<<endl;

} }

void search(int key) void preorder(node *temp)

{ {

int found=0; if( temp!= NULL)

node *temp= root; {

do{ cout<<temp->num<< endl;

if( temp->num==key) preorder(temp->right);


preorder(temp->left); }

}
int main()
}
{
void inorder(node *temp)
int x,y,i,ch;
{
char ask;
if( temp!= NULL)
in:
{
cout<<"how many numbers do you want
// cout<<temp->num<< endl;
to enter?\n";
inorder(temp->right);
cin>>i;
cout<<temp->num<< endl;
while( i>0)
inorder(temp->left);
{
}
cout<<"enter a number\n";
}
cin>>x;

void post_order(node *temp) add(x);

{ i--;

if( temp!= NULL) }

{ node *tree= root;

choice:
post_order(temp->right);
cout<<"enter 1 to display in pre order\n";
post_order(temp->left);
cout<<"enter 2 to display in order\n";
cout<<temp->num<< endl;
cout<<"enter 3 to display in post order\n";
}
cout<<"enter 4 to binary tree search\n";
cout<<"enter 5 to exit\n"; searched\n";

cin>> ch; cin>>y;

search(y);
if(ch==1)
}
{
else if(ch==5)
cout<<"pre order\n";
cout<<"Good bye\n";
preorder(tree);
else
}
cout<<"Invalid choice\n";

else if (ch==2) cout<<"do u wanna back to the

{ menu?(y/n)\n";

cout<<"inorder\n"; cin>>ask;

inorder( tree); if(ask=='y'||ask=='Y')

} {goto choice;}

else if(ch==3) }

cout<<"post_order\n";

post_order( tree);

else if(ch==4)

cout<<"enter the number to be

You might also like