Sunday, 16 October 2011

LINKED LIST FOR SEARCHING, INSERTION & DELETION


PROGRAM:

/*LINKED LIST FOR SEARCHING, INSERTION & DELETION*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*ptr,*new;
void insert_first();                                        /*FUNCTION DECLARATION*/
void disp();
void insert_last();
void insert_middle();
void del_first();
void del_last();
void del_middle();
void search();
int main()
          {
          int x,e,q;
          printf("enter the elements....press -1 once you are done\n");
          scanf("%d",&x);
                   while(x!=-1)
                   {
                   new=(struct node*)malloc(sizeof(struct node));
                    new->data=x;
                   new->next=NULL;
                             if(ptr==NULL)                 /*CREATION OF LINKED LIST*/
                             {
                             ptr=new;
                             head=ptr;
                             }
                             else
                             {
                             ptr->next=new;
                             ptr=new;
                             }
                   scanf("%d",&x);
                   }
          ptr=head;
          printf("\nelements in the list are...\n");
                   while(ptr!=NULL)                          /*DISPLAYING LINKED LIST*/
                   {
                   printf("%d\n",ptr->data);
                   ptr=ptr->next;
                   }

          do
          {
          printf("--------------------------------------------");
          printf("\nENTER THE OPTION\n");
printf("--------------------------------------------");
          printf("\n1.INSERTION-1ST ELEMENT");
          printf("\n2.INSERTION-LAST ELEMENT");
          printf("\n3.INSERTION-AT ANY POSITION");
          printf("\n4.DELETING THE 1ST ELEMENT");
          printf("\n5.DELETING THE LAST ELEMENT");
          printf("\n6.DELETING THE ELEMENT AT ANY POSITION");
          printf("\n7.SEARCH AN ELEMENT\n");
          printf("--------------------------------------------\n");
          scanf("%d",&e);
                   switch(e)
                   {
                   case 1: insert_first();                              /*FUNCTION CALL*/
                             break;
                   case 2: insert_last();
                             break;
                   case 3: insert_middle();
                             break;
                   case 4: del_first();
                             break;
                   case 5: del_last();
                             break;
                   case 6: del_middle();
                             break;
                   case 7:search();
                             break;        
                   default: printf("\nenter the correct option");
                             break;
                   }
         
    printf("\ndo you want to continue???\n");
    printf("press 1 to continue...\n");
    printf("press 2 to quit...\n");
    scanf("%d",&q);
    }while(q==1);
          }       
         
                   void insert_first()                          /*INSERTION AT 1ST NODE*/
                   {
                   int a;
                   struct node *ins;
                   printf("enter the number to be inserted...");
                   scanf("%d",&a);
                   ins=(struct node*)malloc(sizeof(struct node));
                   ins->data=a;
                   ins->next=head;
                   head=ins;
                   disp();
                    }
                  
                   void disp()                                   /*DISPLAY FUNCTION*/
                   {
                             ptr=head;
                             while(ptr!=NULL)
                             {
                             printf("%d\n",ptr->data);
                             ptr=ptr->next;
                             }
                   }
                  
                   void insert_last()                  /*INSERTION AT THE LAST  NODE*/
                   {
                   int b;
                   struct node *ins1;
                   printf("enter the number to be inserted...");
                   scanf("%d",&b);
                   ins1=(struct node*)malloc(sizeof(struct node));
                   ins1->data=b;
                   ins1->next=NULL;
                   ptr=head;
                            
while(ptr->next!=NULL)
                             {
                             ptr=ptr->next;
                             }
                   ptr->next=ins1;
                   disp();
                   }

                   void insert_middle()           /*INSERTION AT ANY NODE*/
                   {
                   int z,y;
                   struct node *ins2;
                   printf("\nenter the number to be inserted...");
                   scanf("%d",&y);
                   printf("\nenter the number in the list after which the number is to be inserted...");
                   scanf("%d",&z);
                   ins2=(struct node*)malloc(sizeof(struct node));
                   ins2->data=y;
                   ins2->next=NULL;
                   ptr=head;
                             while(ptr->data!=z)
                             {
                             ptr=ptr->next;
                             }
                   ins2->next=ptr->next;
                   ptr->next=ins2;
                   disp();
                   }
                  
                   void del_first()               /*DELETION OF 1ST NODE*/
                   {
                   ptr=head;
                   ptr=ptr->next;
                   head=ptr;
                   printf("\nelements after the deletion of the first term\n");
                   disp();
                   }



                    void del_last()             /*DELETION OF LAST NODE*/
                    {
                    ptr=head;
                              while((ptr->next)->next!=NULL)
                             {
                             ptr=ptr->next;
                             }
                   ptr->next=NULL;
                   printf("\nlist after the deletion of the last element\n");
                   disp();
                   }
         
                   void del_middle()           /*DELETION OF ANY NODE*/
                   {
                   int t;
                   printf("enter the element in the list which is to be deleted...");
                   scanf("%d",&t);
                   ptr=head;
                             while((ptr->next)->data!=t)
                             {
                             ptr=ptr->next;
                             }
                   ptr->next=(ptr->next)->next;
                   printf("\nelements in the list after deletion\n");
                   disp();
                   }
         
                   void search()                           /*SEARCHING*/
                   {
                   int l,v=0;
                   printf("enter the element to be searched\n");
                   scanf("%d",&l);
                   ptr=head;
                             while(ptr->next!=NULL)
                             {
                                      if(ptr->data==l)
                                      {
                                      v++;
                                      break;
                                      }
                             ptr=ptr->next;
                             }
                             if(v==1)
                             {       
                             printf("\nthe element is found");
                             }
                             else
                             {
                             printf("\nthe element is not found");
                             }
                   }
OUTPUT:

enter the elements....press -1 once you are done
1
2
3
4
5
6
-1

elements in the list are…
1
2
3
4
5
6
-------------------------------------------------------------------------------
ENTER THE OPTION
-------------------------------------------------------------------------------
1.INSERTION-AT ANY POSITION
2.INSERTION-LAST ELEMENT
3.INSERTION-1ST ELEMENT
4.DELETING THE 1ST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
-------------------------------------------------------------------------------
3




enter the number to be inserted…5
5
1
2
3
4
5
6
do you want to continue???
press 1 to continue...
press 2 to quit...
1
-------------------------------------------------------------------------------
ENTER THE OPTION
-------------------------------------------------------------------------------
1.INSERTION-AT ANY POSITION
2.INSERTION-LAST ELEMENT
3.INSERTION-1ST ELEMENT
4.DELETING THE 1ST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
-------------------------------------------------------------------------------
2
enter the number to be inserted…9
5
1
2
3
4
5
6
9


do you want to continue???
press 1 to continue...
press 2 to quit...
1
-------------------------------------------------------------------------------
ENTER THE OPTION
-------------------------------------------------------------------------------
1.INSERTION-AT ANY POSITION
2.INSERTION-LAST ELEMENT
3.INSERTION-1ST ELEMENT
4.DELETING THE 1ST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
-------------------------------------------------------------------------------
1
enter the number to be inserted...10
enter the number in the list after which the number is to be inserted…2
5
1
2
10
3
4
5
6
9
do you want to continue???
press 1 to continue...
press 2 to quit...
1




-------------------------------------------------------------------------------
ENTER THE OPTION
-------------------------------------------------------------------------------
1.INSERTION-AT ANY POSITION
2.INSERTION-LAST ELEMENT
3.INSERTION-1ST ELEMENT
4.DELETING THE 1ST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
-------------------------------------------------------------------------------
4

elements after the deletion of the first term
1
2
10
3
4
5
6
9
do you want to continue???
press 1 to continue...
press 2 to quit...
1







-------------------------------------------------------------------------------
ENTER THE OPTION
-------------------------------------------------------------------------------
1.INSERTION-AT ANY POSITION
2.INSERTION-LAST ELEMENT
3.INSERTION-1ST ELEMENT
4.DELETING THE 1ST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
-------------------------------------------------------------------------------
5

list after the deletion of the last element

1
2
10
3
4
5
6

do you want to continue???
press 1 to continue...
press 2 to quit...
1







-------------------------------------------------------------------------------
ENTER THE OPTION
-------------------------------------------------------------------------------
1.INSERTION-AT ANY POSITION
2.INSERTION-LAST ELEMENT
3.INSERTION-1ST ELEMENT
4.DELETING THE 1ST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY POSITION
7.SEARCH AN ELEMENT
-------------------------------------------------------------------------------
6
enter the element in the list which is to be deleted...10

elements in the list after deletion

1
2
3
4
5
6

do you want to continue???
press 1 to continue...
press 2 to quit...
1








-------------------------------------------------------------------------------
ENTER THE OPTION
-------------------------------------------------------------------------------
1.INSERTION-AT ANY POSITION
2.INSERTION-LAST ELEMENT
3.INSERTION-1ST ELEMENT
4.DELETING THE 1ST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY  POSITION
7.SEARCH AN ELEMENT
-------------------------------------------------------------------------------
7
enter the element to be searched
4
the element is found
do you want to continue???
press 1 to continue...
press 2 to quit...
1
-------------------------------------------------------------------------------
ENTER THE OPTION
-------------------------------------------------------------------------------
1.INSERTION-AT ANY POSITION
2.INSERTION-LAST ELEMENT
3.INSERTION-1ST ELEMENT
4.DELETING THE 1ST ELEMENT
5.DELETING THE LAST ELEMENT
6.DELETING THE ELEMENT AT ANY  POSITION
7.SEARCH AN ELEMENT
-------------------------------------------------------------------------------
7
enter the element to be searched
9
the  element is not found
do you want to continue???
press 1 to continue...
press 2 to quit...
2

No comments:

Post a Comment