Algorithm Lab Manual Full
Algorithm Lab Manual Full
Searching
/*
Example - 1
which is to be searched from the user . It searches the number in the list
#include<iostream>
int index=0,found=0;
do{
if (x[index]==key)
found=1;
else
index++;
}while(found==0&&index<size);
if(found==1)
else
}
int main()
int list[6],i,num,n;
cin>>n;
for(i=0;i<n;i++)
cin>>list[i];
cin>>num;
linear(list,num,n);
=====================================
The output of the above code is the following
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
*/
#include<iostream>
#include<string>
int index=0,found=0;
do{
if(x[index]==key)
found=1;
else
index++;
}while(found==0&&index<6);
if(found==1)
else
int main()
string list[6],name;
int i;
for(i=0;i<6;i++)
cin>>list[i];
cin>>name;
linear(list,name);
=====================================
The output of the above code is the following
which is to be searched from the user . It searches the number in the list
*/
#include<iostream>
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)
else
int main(){
int list[6],i,num;
for(i=0;i<6;i++)
cin>>list[i];
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
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
int main()
int list[6],i,num;
for(i=0;i<6;i++)
cin>>list[i];
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
*/
#include<iostream>
#include<string>
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)
else
int main(){
string list[6],name;
int i;
for(i=0;i<6;i++)
cin>>list[i];
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
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>
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)
else
int main()
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;
cin>>k;
for(i=0;i<6;i++)
cout<<y[i]<<" , ";
cout<<endl;
for(i=0;i<6;i++)
cout<<x[i]<<" , ";
cout<<endl;
binary(x,k);
=====================================
=====================================
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
*/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>
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)
else
int main()
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])
{goto add;}
else
x[i]=num;
int k;
cin>>k;
for(i=0;i<6;i++)
cout<<x[i]<<",";
cout<<endl;
binary(x,k);
}
=====================================
=====================================
11
15
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
#include <iostream>
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<<x[i]<<" , ";
int main(){
int y[6],i;
for(i=0;i<6;i++)
cin>>y[i];
for(i=0;i<6;i++)
cout<<y[i]<<" , ";
cout<<endl;
bubble(y);
=====================================
=====================================
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
#include <iostream>
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;}
for(i=0;i<6;i++)
cout<<x[i]<<" , ";
int main()
int y[6],i;
for(i=0;i<6;i++)
cin>>y[i];
for(i=0;i<6;i++)
cout<<y[i]<<" , ";
cout<<endl;
bubble(y);
=====================================
=====================================
//---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
#include <iostream>
#include<string>
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;
for(i=0;i<6;i++)
cout<<x[i]<<" , ";
int main()
string x[6];
int i;
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);
=====================================
=====================================
//---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
#include <iostream>
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;
for(i=0;i<6;i++)
cout<<x[i]<<" , ";
int main()
int y[6];
int i;
for(i=0;i<6;i++)
cin>>y[i];
for(i=0;i<6;i++)
cout<<y[i]<<" , ";
cout<<endl;
insertion(y);
}
=====================================
=====================================
4
7
5
0
1
3
4,7,5,0,1,3
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
#include<string>
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<<x[i]<<" , ";
int main()
string y[6];
int i;
for(i=0;i<6;i++)
cin>>y[i];
for(i=0;i<6;i++)
cout<<y[i]<<" , ";
cout<<endl;
insertion(y);
=====================================
=====================================
//---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
#include <iostream>
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;
order\n";
for(i=0;i<6;i++)
cout<<x[i]<<" , ";
}
int main()
int y[6],i;
for(i=0;i<6;i++)
cin>>y[i];
for(i=0;i<6;i++)
cout<<y[i]<<" , ";
cout<<endl;
insertion(y);
=====================================
=====================================
4
7
5
0
1
3
4,7,5,0,1,3
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
#include <iostream>
#include<string>
{
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;
for(i=0;i<6;i++)
cout<<x[i]<<" , ";
int main()
{
string y[6];
int i;
for(i=0;i<6;i++)
cin>>y[i];
for(i=0;i<6;i++)
cout<<y[i]<<" , ";
cout<<endl;
selection(y);
=====================================
=====================================
9
7
5
0
1
3
The numbers before sorted
9,7,5,0,1,3
0,1,3,5,7,9
//---15---
#include <iostream>
#include<string>
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;
for(i=0;i<6;i++)
cout<<x[i]<<" , ";
//---16---
#include <iostream>
#include<string>
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;
for(i=0;i<6;i++)
cout<<x[i]<<" , ";
int main()
char y[6];
int i;
for(i=0;i<6;i++)
cin>>y[i];
for(i=0;i<6;i++)
cout<<y[i]<<" , ";
cout<<endl;
selection(y);
#include<iostream>
struct node{
int num;
node *nxt;
};
int count=0;
void display();
void add_end()
char ask;
do{
node *temp;
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++;
display();
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>
struct node{
int num;
node *nxt;
};
int count=0;
void display();
void addmiddle()
char ask;
int place;
int i;
do{
node *temp1;
node *temp;
cout<<"Enter a number\n";
cin>>temp->num;
if(start==NULL)
{
temp->nxt=NULL;
start= temp;
count=1;
display();
else{
cin>>place;
if(place>count)
do{
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();
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>
struct node{
int num;
node *nxt;
};
int count=0;
void display();
void deleteanywhere()
char ask;
int place;
int i;
int del;
do{
node *temp1;
node *target;
if(start==NULL)
display();
else{
cin>>place;
if(place>count)
do{
cin>>place;
}while(place>count);
target= start;
if(target->nxt==NULL || place==1)
del=target->num;
start=target->nxt;
delete target;
count--;
else
for(i=1;i!=place;i++)
temp1=target;
target=target->nxt;
temp1->nxt=target->nxt;
del=target->num;
delete target;
count--;
display();
cin>>ask;
}while(ask=='y'||ask=='Y');
void add_end()
char ask;
do{
node *temp;
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++;
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>
struct node{
int num;
node *nxt;
};
int count=0;
void display();
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--;
display();
cin>>ask;
}while(ask=='y'||ask=='Y');
void add_start()
char ask;
do{
node *temp;
cout<<"Enter a number\n";
cin>>temp->num;
if(start==NULL)
start= temp;
temp->nxt=NULL;
}
else
temp->nxt=start;
start= temp;
count++;
display();
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>
struct node{
int num;
node *nxt;
};
int count=0;
void display();
void deleteStart()
int del;
char ask;
do{
node *temp;
if(start==NULL)
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--;
display();
cin>>ask;
}while(ask=='y'||ask=='Y');
void add_start()
char ask;
do{
node *temp;
cout<<"Enter a number\n";
cin>>temp->num;
if(start==NULL)
{
start= temp;
temp->nxt=NULL;
else
temp->nxt=start;
start= temp;
count++;
display();
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>
struct node{
int num;
node *nxt;
};
int count=0;
void display();
void sort()
int temp,ch;
node *t1,*t2,*t3,*t4;
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;
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++;
display();
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
#include<iostream>
struct node{
int num;
node *nxt;
};
int count=0;
void display();
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++;
display();
cin>>ask;
}while(ask=='y'||ask=='Y');
void addmiddle()
{
char ask;
int place;
int i;
do{
node *temp1;
node *temp;
cout<<"Enter a number\n";
cin>>temp->num;
if(start==NULL)
temp->nxt=NULL;
start= temp;
count=1;
display();
else{
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++;
display();
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)
display();
else{
cin>>place;
if(place>count)
do{
cin>>place;
}while(place>count);
target= start;
if(target->nxt==NULL || place==1)
{
del=target->num;
start=target->nxt;
delete target;
count--;
else
for(i=1;i!=place;i++)
temp1=target;
target=target->nxt;
temp1->nxt=target->nxt;
del=target->num;
delete target;
count--;
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;
cout<<"Enter a number\n";
cin>>temp->num;
if(start==NULL)
start= temp;
temp->nxt=NULL;
else
temp->nxt=start;
start= temp;
}
count++;
display();
cin>>ask;
}while(ask=='y'||ask=='Y');
void deleteEnd()
int del;
char ask;
do{
node *temp;
if(start==NULL)
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--;
display();
cin>>ask;
}while(ask=='y'||ask=='Y');
void deleteStart()
int del;
char ask;
do{
node *temp;
if(start==NULL)
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--;
display();
cin>>ask;
}while(ask=='y'||ask=='Y');
void sort()
int temp,ch;
node *t1,*t2,*t3,*t4;
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 5 to display\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";
#include<iostream>
struct node{
int num;
node *nxt;
node *prv;
};
struct node *start=NULL;
node *current;
void display();
void addend()
char ask;
do{
node *temp;
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();
cin>>ask;
}while(ask=='y'||ask=='Y');
void deleteend()
char ask;
int del;
do{
node *temp;
node *target=current;
if(start==NULL)
{
del=target->num;
start=NULL;
current->nxt=NULL;
current->prv=NULL;
del=target->num;
current=current->prv;
current->nxt=NULL;
delete target;
else
while(target->nxt!= NULL)
temp=target;
target=target->nxt;
del= target->num;
temp->nxt=NULL;
delete target;
display();
cin>>ask;
}while(ask=='y'||ask=='Y');
void deletestart()
char ask;
int del;
do{
node *temp;
node *target=current;
if(start==NULL)
del=target->num;
start=NULL;
current->nxt=NULL;
current->prv=NULL;
del=target->num;
current=current->nxt;
current->prv=NULL;
delete target;
else
while(target->prv!= NULL)
temp=target;
target=target->prv;
del= target->num;
temp->prv=NULL;
delete target;
display();
cin>>ask;
}while(ask=='y'||ask=='Y');
void addstart()
char ask;
do{
node *temp;
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();
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 5 to display\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>
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)
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();
cin>>ask;
if(ask=='y'||ask=='Y')
{goto add;}
else
{goto menu;}
else if(ch==2)
{
del:
pop();
display();
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";
{goto menu;}
return 0;
}
//---25---
#include<iostream>
struct node{
int num;
node *nxt;
};
void display();
node *top=NULL;
void push()
char ask;
do{
node *temp;
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();
cin>>ask;
}while(ask=='y'||ask=='Y');
void pop()
char ask;
int del_val;
do{
if(start==NULL)
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;
display();
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>
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)
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();
cin>>ask;
if(ask=='y'||ask=='Y')
{goto add;}
else
{goto menu;}
else if(ch==2)
del:
dequeue();
display();
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";
{goto menu;}
return 0;
}
//---27---
#include <iostream>
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)
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();
cin>>ask;
if(ask=='y'||ask=='Y')
{goto add;}
else
{goto menu;}
}
else if(ch==2)
del:
dequeue();
display();
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";
{goto menu;}
}
return 0;
#include<iostream>
struct node{
int num;
node *nxt;
};
void display();
node *rear=NULL;
node *front=NULL;
void enqueue()
char ask;
do{
node *temp;
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();
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;
display();
}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>
struct node{
int num;
node *left;
node *right;
};
void display();
void add(int n)
node *temp;
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)
else
target= target->left;
if(target!= NULL)
{goto in1;}
else
int found=0;
do{
if( temp->num==key)
found=1;
else
temp= temp->right;
else
temp=temp->left;
else
cout<<temp->num<< endl;
preorder(temp->right);
preorder(temp->left);
// cout<<temp->num<< endl;
inorder(temp->right);
cout<<temp->num<< endl;
inorder(temp->left);
}
post_order(temp->right);
post_order(temp->left);
cout<<temp->num<< endl;
int main()
int x,y,i,ch;
char ask;
in:
cin>>i;
while( i>0)
cout<<"enter a number\n";
cin>>x;
add(x);
i--;
choice:
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)
cin>>y;
search(y);
else if(ch==5)
cout<<"Good bye\n";
else
cout<<"Invalid choice\n";
cin>>ask;
if(ask=='y'||ask=='Y')
{goto choice;}