// LINEAR SEARCH
# include<iostream.h>
# include<conio.h>
int search;
class linear_s
{
public: int flag;
input( int *, int , int);
int n, key;
int list[200];
public: void linear_search(int * , int , int ); // prototype
void display(int *, int);
};
// definition of function
void linear_s :: linear_search(int l[], int n, int element)
{
flag = 1;
for( int k = 0; k< n; k++)
{
if(l[k] == element)
{
cout <<"\n Search is successful \n";
cout <<"\n Element: " <<element
<< " " <<"Found at Location : "<< (k+1);
flag = 0 ;
}
}
if (flag)
cout<< "\n Search is unsuccessful";
}
void linear_s :: display(int list[], int n)
{
for( int i = 0 ; i < n ; i++)
{
cout<<" "<< list[i];
}
}
linear_s :: input(int list[], int number, int key)
{
cout <<"Input the number of elements in the list:";
cin >> number;
cout <<"\n Number of elements in the list is :"<<number<<"\n";
for( int i = 0 ; i < number ; i++)
{
cout<<" Input the elements of the list : "<<i+1<<" : ";
cin >> list[i];
}
cout<<"\n Element to be searched :";
cin>>key;
search = key;
return number;
}
void main()
{
linear_s sort;
int number, key, list[200];
number = sort.input( list, number, key);
key = search ;
cout<<"\n Entered list as follows:\n";
sort.display(list,number);
sort.linear_search(list, number, key);
cout<<"\n In the following list\n";
sort.display(list,number);
}
/*
input(int list[], int number, int key)
{
cout <<"Input the number of elements in the list:";
cin >> number;
cout <<"\n Number of elements in the list is :"<<number;
for( int i = 0 ; i < number ; i++)
{
cout<<"\nInput the elements of the list : "<<i+1<<" : ";
cin >> list[i];
}
cout<<"\n Element to be searched :";
cin>>key;
search = key;
return number;
}
*/
# include<iostream.h>
# include<conio.h>
int search;
class linear_s
{
public: int flag;
input( int *, int , int);
int n, key;
int list[200];
public: void linear_search(int * , int , int ); // prototype
void display(int *, int);
};
// definition of function
void linear_s :: linear_search(int l[], int n, int element)
{
flag = 1;
for( int k = 0; k< n; k++)
{
if(l[k] == element)
{
cout <<"\n Search is successful \n";
cout <<"\n Element: " <<element
<< " " <<"Found at Location : "<< (k+1);
flag = 0 ;
}
}
if (flag)
cout<< "\n Search is unsuccessful";
}
void linear_s :: display(int list[], int n)
{
for( int i = 0 ; i < n ; i++)
{
cout<<" "<< list[i];
}
}
linear_s :: input(int list[], int number, int key)
{
cout <<"Input the number of elements in the list:";
cin >> number;
cout <<"\n Number of elements in the list is :"<<number<<"\n";
for( int i = 0 ; i < number ; i++)
{
cout<<" Input the elements of the list : "<<i+1<<" : ";
cin >> list[i];
}
cout<<"\n Element to be searched :";
cin>>key;
search = key;
return number;
}
void main()
{
linear_s sort;
int number, key, list[200];
number = sort.input( list, number, key);
key = search ;
cout<<"\n Entered list as follows:\n";
sort.display(list,number);
sort.linear_search(list, number, key);
cout<<"\n In the following list\n";
sort.display(list,number);
}
/*
input(int list[], int number, int key)
{
cout <<"Input the number of elements in the list:";
cin >> number;
cout <<"\n Number of elements in the list is :"<<number;
for( int i = 0 ; i < number ; i++)
{
cout<<"\nInput the elements of the list : "<<i+1<<" : ";
cin >> list[i];
}
cout<<"\n Element to be searched :";
cin>>key;
search = key;
return number;
}
*/
No comments:
Post a Comment