//searching an element from sorted link list
#include<iostream.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *link;
}NODE;
void main()
{
NODE *ptr,*prev,*start;
start=NULL;
clrscr();
cout<<"\nEnter 10 elements in array\n";
for(int i=0;i<10;i++)
{
ptr=new NODE();
cin>>ptr->data;
ptr->link=NULL;
if(start==NULL)
{
start=ptr;
}
else
{
prev->link=ptr;
}
prev=ptr;
}
//searching elements from sort list
int item;
cout<<"\nEnter element to be search \n";
cin>>item;
ptr=start;
int f=0;
NODE *loc;
loc=NULL;
while(ptr!=NULL)
{
if(item>ptr->data)
{
ptr=ptr->link;
}
else if(item==ptr->data)
{
f=1;
loc=ptr;
break;
}
else
{
loc=NULL;
break;
}
}
if(f==1)
cout<<"\nitem searched successfully at "<<loc;
else
cout<<"\nitem not find at"<<loc;
getch();
}
******output*******
Enter 10 elements in array
2
3
14
25
35
62
75
80
82
85
Enter element to be search
82
item searched successfully at 0x8fa50f5a
No comments:
Post a Comment