//Searching In Sorted Linked List
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
int main()
{
node *start,*p,*pp,*temp;
start=p=NULL;
//Creating Linked List
int n;
cout<<"Press [0] to Exit :: Input Elements To List :";
do
{
cin>>n;
if(n)
{
temp=new node;
temp->info=n;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
}
}while(n);
//printing nodes
p=start;
cout<<"Created Nodes are : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
//Check Accending or descending
pp=start;p=start->link;
if (pp->info<p->info)
{
cout<<"\nAsecnding list \n";
/////Search location
cout<<"Enter Item to Search :";
int item;
cin>>item;
int loc=0,k=1;
if(item<p->info)
{
cout<<"Not Present" ;
exit(0);
}
while(p!=NULL)
{
if(item==p->info)
{
k++;
loc=k;
break;
}
else
{
p=p->link;
k++;
}
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}
else
{
cout<<"\nDescending List \n";
cout<<"Enter Item to Search :";
int item;
cin>>item;
int loc=0,k=1;
if(item>p->info)
{
cout<<"Not Present" ;
exit(0);
}
while(p!=NULL)
{
if(item==p->info)
{
k++;
loc=k;
break;
}
else
{
p=p->link;
k++;
}
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}
return 0;
}
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
int main()
{
node *start,*p,*pp,*temp;
start=p=NULL;
//Creating Linked List
int n;
cout<<"Press [0] to Exit :: Input Elements To List :";
do
{
cin>>n;
if(n)
{
temp=new node;
temp->info=n;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
}
}while(n);
//printing nodes
p=start;
cout<<"Created Nodes are : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
//Check Accending or descending
pp=start;p=start->link;
if (pp->info<p->info)
{
cout<<"\nAsecnding list \n";
/////Search location
cout<<"Enter Item to Search :";
int item;
cin>>item;
int loc=0,k=1;
if(item<p->info)
{
cout<<"Not Present" ;
exit(0);
}
while(p!=NULL)
{
if(item==p->info)
{
k++;
loc=k;
break;
}
else
{
p=p->link;
k++;
}
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}
else
{
cout<<"\nDescending List \n";
cout<<"Enter Item to Search :";
int item;
cin>>item;
int loc=0,k=1;
if(item>p->info)
{
cout<<"Not Present" ;
exit(0);
}
while(p!=NULL)
{
if(item==p->info)
{
k++;
loc=k;
break;
}
else
{
p=p->link;
k++;
}
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}
return 0;
}