Sunday 27 January 2013

Write a program to Search element in Unsorted Singular Linked List.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
               int data;
               struct node *link;
}*start=NULL;

               void insert_beg()
               {
               char ch;
               do
               {
                               if(start==NULL)
                               {
                                              start=new node;
                                              cout<<"Enter Data:-";
                                              cin>>start->data;
                                              start->link=NULL;
                               }
                               else
                               {
                                              struct node *temp,*ptr;
                                              temp=new node;

                                              cout<<"enter Data:-";
                                              cin>>temp->data;
                                              temp->link=start;
                                              start=temp;
                               }
                               cout<<"Do u want to continue(y/n)";
                               cin>>ch;
               }while(ch=='y');
               }

               int search(int n)
               {
                               int f=0,c=0;
                               struct node *ptr;
                               ptr=start;
                               while(ptr!=NULL)
                               {
                                              c++;
                                              if(ptr->data==n)
                                              {
                                                             f=1;
                                                             break;
                                              }
                                              else
                                              {
                                                             ptr=ptr->link;
                                              }
                               }//end of while loop
                               if(f==1)
                               {
                                              return(c);
                               }
                               else
                               {
                                              return(-1);
                               }
               }


               void display()
               {
                               struct node *ptr;
                               ptr=start;
                               cout<<endl<<"linked list is="<<endl;
                               while(ptr!=NULL)
                               {
                                              cout<<ptr->data;
                                              ptr=ptr->link;
                               }
               }


void main()
{
               clrscr();
               int choice;
               int n,loc,c=0;
               cout<<" press 1 for insert at the begining"<<endl;
               cout<<" press 2 for search the element in unsorted linked list"<<endl;
               cout<<" press 3 for display linked list"<<endl;
               cout<<" press 4 for EXIT"<<endl;
               while(1)
               {
                               cout<<" Choice your option:-";
                               cin>>choice;
                               switch(choice)
                               {
               case 1:
                               insert_beg();
                               break;
               case 2:
                               cout<<"Enter which value u want to search:-";
                               cin>>n;
                               c=search(n);
                               if(c!=-1)
                               {
                                              cout<<"\n Value found at position:-"<<c;
                               }
                               else
                               {
                                              cout<<"Not found";
                               }
                                              break;
               case 3:
                               display();
                               break;
               case 4:
                               exit(0);
                               break;
               default:
                               cout<<"try again";
                               break;
               }//END OF SWITCH CASE

               getch();
               }//END OF WHILE
}//END OF MAIN

No comments:

Post a Comment