Skip to main content

Featured

functions in Python

C++ program to search item in linked list


//This program searches for any item in the linked list.
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *next;
};
void main ()
{
clrscr();
node *start,*save,*newptr;
start=NULL;
char ans;
do{
   node *newptr=new node;
   if(start==NULL)
   {
   cout<<"Enter Info for the new node\n";
   cin>>newptr->info;
   start=newptr;
   newptr->next=NULL;
   save=start;
   }
   else
   {cout<<"Enter Info for the new node\n";
   cin>>newptr->info;
   save->next=newptr;
   newptr->next=NULL;
   save=newptr;
   }
   cout<<"Wanna Insert new node ?(y/n)\n";
   cin>>ans;
  }while(ans=='y'||ans=='Y');
node *ptr;
int pos=1;
int item;
cout<<"Enter the item to be searched\n";
cin>>item;
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
if(ptr->info==item)
{
 cout<<"Item Found at Node "<<pos<<endl;
 break;
}
pos++;
}
if(ptr==NULL)
{cout<<"Item not found\n";}
getch();
}

Comments

Popular Posts