Algorithm Lab Manual
Algorithm Lab Manual
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)
}while(found==0&&index<6);
if(found==1)
A linear search code which accept n cout<<key<< " is not found in the
#include<iostream> int 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 }
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
cin>>list[i]; {
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<<" enter 6 numbers in descending the name in the list and then displays
for(i=0;i<6;i++) */
cin>>list[i]; #include<iostream>
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
int main(){
string list[6],name; /*
int i; Example - 6
cout<<" enter sorted 6 names in ascending A binary search code which accept n
do{ {
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) }
else {
} 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
for(i=0;i<6;i++)
cout<<x[i]<<" , ";
cout<<endl;
binary(x,k);
//---7---
}
*/a binary search code which guide the
following #include<iostream>
9 int left=0,found=0,right=5,mid;
7
int index;
5
do{ {
{ 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); {
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
==============================
=======
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*/
{ cin>>y[i];
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-1]=temp; following
#include <iostream> {
{ for(i=0;i<6;i++)
{ for(i=0;i<6;i++)
{ cout<<endl;
if(x[j]<x[j-1]) bubble(y);
{ }
temp=x[j]; ==============================
x[j]=x[j-1]; =======
cout<<endl;} =======
temp=x[j];
//---10--- x[j]=x[j-1];
}
//in descending order
cout<<"the names after sorted in
#include <iostream>
descending order\n";
#include<string>
{ }
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---
The output of the above code is the unsorted numbers from the keyboard and
======= sorted*/
temp=x[i]; cin>>y[i];
{
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]<<" , ";
int main() 4
7
{
5
int y[6]; 0
1
int i; 3
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;
descending order then it displays them cout<<"the names after sorted using
#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;
insertion(y);
==============================
=======
//---13---
The output of the above code is the
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-1]=temp; cout<<endl;
} insertion(y);
}
for(int k=0;k<6;k++)
cout<<x[k]<<" , ";
cout<<endl;
} ==============================
insertion sort in descending order\n"; The output of the above code is the
following
for(i=0;i<6;i++)
==============================
cout<<x[i]<<" , ";
=======
}
{ 4
7
int y[6],i;
5
cout<<"enter 6 unsorted numbers\n"; 0
1
3
{
4,7,5,0,1,3
string temp;
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*/
}
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---
} #include<string>
======= {
{ cout<<endl;
cout<<"enter a name\n"; }
cin>>num; i++;
y[i]=num; }
x[i]=num; for(i=0;i<6;i++)
while(j>=0) cout<<endl;
if(x[j]<num) for(i=0;i<6;i++)
{ cout<<x[i]<<" , ";
x[j+1]=num; }
break; //---16---
} #include <iostream>
else #include<string>
x[j]=num; {
} int i,j,small;
char temp;
j--;
for(i=0;i<6;i++) char y[6];
{ int i;
for(j=i;j<6;j++) for(i=0;i<6;i++)
{ cin>>y[i];
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++;
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;
{ node *temp1;
cout<<"Enter a number\n";
cin>>temp->num;
//---18---
// add in the middle
if(start==NULL)
#include<iostream> {
}; display();
int count=0; }
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++;
display(); {
} addmiddle();
cin>>ask;
}while(ask=='y'||ask=='Y');
//---19---
} //delete anywhere
do{ {
delete target;
if(start==NULL)
cout<<del<<" is deleted succefully\n";
{
count--;
display(); {
} for(i=1;i!=place;i++)
else{ {
} temp->nxt=NULL;
temp1->nxt=target->nxt; if(start==NULL)
} while(target->nxt!= NULL)
display(); target->nxt=temp;
} }
}while(ask=='y'||ask=='Y'); display();
{ }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;
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;
//delete end {
#include<iostream> del=temp->num;
else {
{ char ask;
temp=temp->nxt; cin>>temp->num;
} if(start==NULL)
del= temp->num; {
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');
}while(temp!=NULL); {
}} char ask;
{ node *temp;
add_start(); if(start==NULL)
{ }
if(temp->nxt==NULL) {
{ char ask;
del=temp->num; do{
} cin>>temp->num;
else if(start==NULL)
{ {
delete temp; }
cout<<del<<" is deleted\n";
else
}}
{
count--;
temp->nxt=start;
cout<<count<<" : nodes has the list\n";
start= temp;
display();
}
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;
cout<<temp->num<<"-->"; {
}} display();
{ for(t1=start;t1!=NULL ;t1=t1->nxt)
{ }
for(t3=start,t2=t3- }
>nxt;t2!=NULL;t3=t2,t2=t2->nxt) }
{ display();
if(start==t3) }
{ void add_start()
start=t2; {
{ cin>>temp->num;
t4=start; if(start==NULL)
while(t4->nxt!=t3) {
t3->nxt=t2->nxt; temp->nxt=NULL;
t2->nxt=t4->nxt; }
t4->nxt=t2;
else
}
{
temp->nxt=start; }while(temp!=NULL);
} }}
display(); add_start();
cin>>ask; }
}while(ask=='y'||ask=='Y');
//---23---
}
/* A single linked list code which
{ program
cout<<temp->num<<"-->";
#include<iostream>
temp=temp->nxt;
using namespace std; while(target->nxt!= NULL)
node *nxt; }
}; count++;
{ }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{
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";
if(place>count) {
}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);
}
{ }
start=target->nxt; cin>>ask;
{ }while(ask=='y'||ask=='Y');
char ask; }
node *temp; {
cin>>temp->num; do{
{ if(start==NULL)
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 *temp;
target=temp; if(start==NULL)
} else
del= temp->num; {
cout<<del<<" is deleted\n"; {
} del=temp->num;
} start=NULL;
display(); }
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--; {
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 9 to exit\n";
cout<<"start-->"; cin>>ch;
temp=start; if(ch==1)
do{ {
cout<<temp->num<<"-->"; add_end();
}while(temp!=NULL); }
}} {
{ {goto menu;}
int ch; }
menu: {
deleteEnd(); }
} {
{ {goto menu;}
deleteStart(); }
} cout<<"good bye\n";
{ 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)
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";
temp->nxt=NULL; {
{ int del;
current->prv=NULL;
if(start==NULL)
}
cout<<"the list is empty\n";
else
start=NULL; temp->nxt=NULL;
} display();
>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)
target=target->nxt;
else if(target->prv==NULL&& current-
>nxt==NULL) target=target->prv;
{ }
start=NULL; temp->prv=NULL;
} display();
>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;
current=temp; cout<<"NULL<---(current)--->NULL\n";
current->nxt=NULL; else
current->prv=NULL; {
} cout<<"NULL<---";
else temp=current;
{ while(temp->prv!=NULL)
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;
{ cout<<"NULL\n";
}} else if(ch==3)
int main() {
{ deleteend();
menu: }
} #include <iostream>
using namespace std; top--;
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;
{ {
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> }
struct node{ {
}; }
{ }
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();
{ 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)
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(){
{ char ask;
int i; menu:
{ cout<<"enter 4 to exit\n";
if(que[i]==NULL) cin>>ch;
else {
{ add:
if(i==rear) enqueue(n);
cout<<"<---rear"; display();
if(ask=='y'||ask=='Y') else
{goto add;} {
} {goto menu;}
}
else if(ch==2)
return 0;
{
}
del:
dequeue();
display(); #include<iostream>
{goto del;} };
} node *rear=NULL;
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;
front=start; cout<<"null\n";
} }}
display(); {
} int ch;
cin>>ask; menu:
} 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();
{ {
} 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) {
else temp=temp->left;
{ }
} is found"<<endl;
} else
} }
{ {
}
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;
{ i--;
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";
search(y);
if(ch==1)
}
{
else if(ch==5)
cout<<"pre order\n";
cout<<"Good bye\n";
preorder(tree);
else
}
cout<<"Invalid choice\n";
{ menu?(y/n)\n";
cout<<"inorder\n"; cin>>ask;
} {goto choice;}
else if(ch==3) }
cout<<"post_order\n";
post_order( tree);
else if(ch==4)