PROGRAM TO SEARCH AN ELEMENT IN A CIRCULAR LINKED LIST

/*PROGRAM TO SEARCH AN ELEMENT IN A CIRCULAR LINKED LIST*/
#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *start=NULL,*ptr,*temp;
int num,item,c=0,count=0;
char ch;
clrscr();
cout<<“Inserting elements in the list:”<<endl;
do
{
temp=new node;
cout<<“Enter element: “;
cin>>num;
temp->data=num;
if(start==NULL)
{
start=temp;
start->link=start;
}
else if(start->link==start)
{
start->link=temp;
temp->link=start;
}
else
{
ptr=start;
while(ptr->link!=start)
{
ptr=ptr->link;
}
temp->link=ptr->link;
ptr->link=temp;
}
cout<<“Do you want to continue?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
cout<<“Elements in the list are:”<<endl;
cout<<start->data<<“\t”;
for(ptr=start->link;ptr!=start;ptr=ptr->link)
{
cout<<ptr->data<<“\t”;
}
cout<<endl<<“Enter the element you want to search: “;
cin>>item;
if(item==start->data)
{
cout<<“Element found at 1 node”<<endl;
count=1;
}
c=1;
for(ptr=start->link;ptr!=start;ptr=ptr->link)
{
c++;
if(item==ptr->data)
{
count++;
cout<<“Element found at “<<c<<” node”<<endl;
}
}
if(count==0)
cout<<“Element is not present in the list”;
else
cout<<“Element found “<<count<<” times in the list”;
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *