Breaking News
recent

WAP to search an element from a linked list

#include<iostream.h>
#include<conio.h>
#include<process.h>
class node
{
                public:
                                int data ;
                                node *next;
};

void main()
{ node *start,*p,*beg,*end,*loc;
char ch;
int j=0,item;
clrscr();
start='\0';
do
{
                if(start=='\0')
                { start = new node;
                  p= start;
                }
                else
                { p->next=new node;
                  p=p->next;
                }
               

cout<<"\nEnter the Element\t";
                cin>>p->data;
j++;
cout<<"\nTo continue enter your choice(y/n)? ";
cin>>ch;
}while(ch=='y'||ch=='Y');
p->next='\0';

cout<<"\nElement for nodes is";
p=start;
while(p!=0)
{ cout<<"\t"<<p->data;
  p=p->next;
}
cout<<"\nNumber of nodes is "<<j;

cout<<"\nEnter the element to searched: ";
cin>>item;
p=start;
while(p!=0)
{ if (item==p->data)
{ loc=p;
cout<<"\nSearch Successful ----------Element found at location: "<<loc;
getch();
exit(0);
}
else
{ p=p->next; }
}

loc='\0';
cout<<"\nSearch Unsuccessful";
getch();

}

Output:


Unknown

Unknown

No comments:

Post a Comment

Powered by Blogger.