Sunday, October 21, 2012

SINGLY LINKED LIST and Different OPERATION on it





#include
#include
struct node {
       int data;
       struct node *next;
       }*start;
      
main()
{
      int choice,n,m,position,i;
      start=NULL;
      while(1)
      {
              printf("1.Create List\n");
              printf("2.Add at begining\n");
              printf("3.Add after\n");
              printf("4.Delete\n");
              printf("5.Display\n");
              printf("6.Count\n");
              printf("7.Reverse\n");
              printf("8.Search\n");
              printf("9.Quit\n");
              printf("Enter Your Choice:");
              scanf("%d",&choice);
              switch(choice)
              {
                            case 1:
                                 printf("How many nodes you want:");
                                 scanf("%d",&n);
                                 for(i=0;i                                 {
                                                 printf("Enter the Element: ");
                                                 scanf("%d",&m);
                                                 create_list(m);
                                 }
                                 break;
                            case 2:
                                 printf("Enter the element :");
                                 scanf("%d",&m);
                                 addatbeg(m);
                                 break;
                            case 3:
                                 printf("Enter the element: ");
                                 scanf("%d",&m);
                                 printf("Enter the position after which element is inserted: ");
                                 scanf("%d",&position);
                                 addafter(m,position);
                                 break;
                            case 4:
                                 if(start==NULL)
                                 {
                                            printf("List is empty\n");
                                            continue;
                                 }
                                 printf("Enter the element for deletion:");
                                 scanf("%d",&m);
                                 del(m);
                                 break;
                            case 5:
                                 display();
                                 break;
                            case 6:
                                 count();
                                 break;
                            case 7:
                                 rev();
                                 break;
                            case 8:
                                 printf("Enter the element need to be searched:");
                                 scanf("%d",&m);
                                 search(m);
                                 break;
                            case 9:
                                 exit(1);
                            default:
                                 printf("Wrong Choice\n");
                      }
              }
   
      }
create_list(int num)
{
   struct node *temp,*q;
   temp = malloc(sizeof(struct node));
   temp->data=num;
   temp->next=NULL;
   if(start == NULL)
    {
        start=temp;
    }
    else
    {
        q=start;
        while(q->next!=NULL)
            q=q->next;
        q->next=temp;                           
    }
}
addatbeg(int num)
{
        struct node *temp;
        temp=malloc(sizeof(struct node));
        temp->data=num;
        temp->next=start;
        start=temp;
}
addafter(int num, int pos)
{
        struct node *temp ,*q;
        int count=1;
        temp=malloc(sizeof(struct node));
        temp->data=num;
        q=start;
        while(q!=NULL)
        {
         if(pos==count)
         {
                       temp->next=q->next;
                       q->next=temp;
         }
         count++;
         q=q->next;
        }
}       
del(int num)
{
        struct node *temp,*q;
        if(start->data==num)
        {
                       temp =start;
                       start=start->next;
                       free(temp);
                       return;
        }
        q=start;
        while(q->next->next!=NULL)
        {
         if(q->next->data==num)
             {
                                   temp=q->next;
                                   q->next=temp->next;
                                   free(temp);
                                   return;
              }       
              q=q->next;
        }
        if(q->next->data==num)
        {
                              temp=q->next;
                              free(temp);
                              q->next=NULL;
                              return;
        }
        printf("Element not found in the List\n");
}      

display()
{
         struct node *q;
         if(start==NULL)
         {
                        printf("Empty List\n");
                        return;
         }
         q=start;
         printf("Elements are:");
         while(q!=NULL)
         {
                             if(q->next!=NULL)
                             printf("%d,",q->data);
                             else
                             printf("%d.\n",q->data);
                             q=q->next;
           
         }
         printf("\n");
}       
count()
{
       struct node *q;
       int cnt;
       if(start==NULL)
       {
                      printf("Zero Element\n");
                      return;
       }
       q=start;
       while(q!=NULL)
       {
            cnt++;
            q=q->next;
                      
       }
       printf("The number of Elements in the list:%d\n",cnt);
}
rev()
{  
     struct node *p1,*p2,*p3;
     if(start->next==NULL)
     {
                          printf("Only one Element is present\n");
                          return;
     }
     if(start->next->next ==NULL)
     {
                          p1 = start;
                          p2= p1->next;
                          p2->next= p1;
                          p1->next=NULL;
                          start = p2;
                          return;
     }
     p1=start;
     p2=p1->next;
     p3=p2->next;
     p1->next=NULL;
     p2->next=p1;
     while(p3!=NULL)
     {
                    p1=p2;
                    p2=p3;
                    p3=p3->next;
                    p2->next=p1;
     }
     start=p2;                                        
}
search(int num)
{
    struct node *q;
    q=start;
    int pos =1;
    while(q!=NULL)
    {
           if(q->data==num)
           {
                    printf("Element %d found at pos=%d\n",num,pos);
                    return;
           }
           pos++;
           q=q->next;
    }
    if(q==NULL)
    printf("Element %d nod found in the list\n",num);
}

No comments:

Post a Comment