Singly Linked List C Program Data Structures

This Program is a Variation of Singly Linked List in C, and is a part of  Mumbai University MCA Colleges Data Structures C Program MCA Sem 2.

#include<stdio.h>
#include<conio.h>

struct list* creat(struct list *);
void concat(struct list *,struct list*);
struct list* sort(struct list *);
void display();
void intersection(struct list *,struct list*);
void uniion(struct list *,struct list*);
void merge(struct list *,struct list*);

struct list
{
          int info;
          struct list *link;
};

int size,item;
void main()
{
          struct list *list1,*end,*list2,*loc,*temp;
          int choice,i,l;
          list1=list2=NULL;
          clrscr();
printf("===================== SINGLY LINKED LIST II =============");
          while(1)
          {
printf("\n\n------------------------ MAIN MENU --------------\n\n");
                   printf("1.CREATE\n\n");
                   printf("2.DISPLAY\n\n");
                   printf("3.CONCAT\n\n");
                   printf("4.MERGE\n\n");
                   printf("5.INTERSECTION\n\n");
                   printf("6.UNION\n\n");
                   printf("7.EXIT\n\n");
                   printf("\n\nEnter your choice:");
                   scanf("%d",&choice);
                   switch(choice)
                   {
                             case 1:
printf("\n\n~~~~~~~~~~~~ ENTER YOUR CHOICE ~~~~~~~~~~~~~~\n\n");
                                      printf("1.CREATE FIRST LIST\n\n");
                                      printf("2.CREATE SECOND LIST\n\n");
                                      printf("Enter the option:");
                                      scanf("%d",&l);
                                      if(l==1)
                                                list1=creat(list1);
                                      else
                                                list2=creat(list2);
                                      break;
                             case 2:
printf("\n\n~~~~~~~~~~~~ ENTER YOUR CHOICE ~~~~~~~~~~~~~~\n\n");
                                      printf("1.DISPLAY FIRST LIST\n\n");
                                      printf("2.DISPLAY SECOND LIST\n\n");
                                      printf("Enter the option:");
                                      scanf("%d",&l);
                                      if(l==1)
                                                display(list1);
                                      else
                                                display(list2);
                                      break;
                             case 3:
                                      concat(list1,list2);
                                      break;

                             case 4:
                                      merge(list1,list2);
                                      break;

                             case 5:
                                      intersection(list1,list2);
                                      break;

                             case 6:
                                      uniion(list1,list2);
                                      break;

                             case 7:
                                      exit();

                             default:
printf("\n\nINVALID ENTRY !!!!!! TRY AGAIN\n\n");
                                      break;
                   }
          }
 }

struct list* creat(struct list *start)
{
          struct list *temp,*end;
          int i;
          if(start==NULL)
          {
                   printf("\n\nEnter the size of list :");
                   scanf("%d",&size);
                   for(i=0;i<size;i++)
                   {
                             printf("\n\nEnter element to add in the list :");
                             scanf("%d",&item);
                             temp=(struct list *) malloc(sizeof(struct list));
                             if(temp==NULL)
                             {
                                      printf("\n\n---------------------------------------\n\n");
                                      printf("SPACE IS NOT AVAILABLE");
                                      printf("\n\n---------------------------------------\n\n");
                             }
                             else
                             {
                                      temp->info=item;
                                      temp->link=NULL;
                                      if(start==NULL)
                                                start=end=temp;
                                      else
                                      {
                                                end->link=temp;
                                                end=temp;
                                      }
printf("\n\n~~~~~~ ELEMENT %d ADDS IN LIST ~~~~~~~~~~\n\n",item);
                                      temp=NULL;
                             }
                   }

          }
          else
          {
                   printf("\n\n---------------------------------------\n\n");
                   printf("\n\nLINKED LIST ALREADY CREATED\n");
                   printf("\n\n---------------------------------------\n\n");
          }
          return(start);
}


void display(struct list *start)
{
          int i;
          struct list *temp,*end;
          temp=NULL;
          if(start==NULL)
                    printf("\n\nLIST HAS NO ELEMENT !!!\n\n");
          else
          {
printf("\n\n~~~~~~~~~~~~~~~~~~~~ LIST ELEMENTS ~~~~~~~~~~~~~~~~~\n\n");
                   temp=start;
                   while(temp!=NULL)
                   {
                             printf("%d\t",temp->info);
                             temp=temp->link;
                   }
          }
}

struct list * sort(struct list *temp)
{
          struct list *ptr1,*ptr2,*ptr3,*ptr4,*ptr5;
          int i=0;
          if(temp!=NULL)
          {
                   if(temp->link!=NULL)
                   {
                             ptr1=temp;
                             ptr5=NULL;
                             while(ptr1->link!=NULL)
                             {
                                      ptr2=ptr1->link;
                                      ptr4=NULL;
                                      while(ptr2!=NULL)
                                      {
                                                if(ptr1->info>ptr2->info)
                                                {
                                                          ptr3=ptr2->link;
                                                          if(ptr4!=NULL)
                                                          {
                                                                   ptr4->link=ptr1;
                                                                   ptr2->link=ptr1->link;
                                                          }
                                                          else
                                                                   ptr2->link=ptr1;
                                                          ptr1->link=ptr3;
                                                          ptr3=ptr2;
                                                          ptr2=ptr1;
                                                          ptr1=ptr3;
                                                          ptr3=NULL;
                                                }
                                                if(ptr5!=NULL)
                                                          ptr5->link=ptr1;
                                                ptr4=ptr2;
                                                ptr2=ptr2->link;

                                      }
                                      if(i==0)
                                                temp=ptr1;
                                      ptr5=ptr1;
                                      ptr1=ptr1->link;
                                      i++;
                             }
                   }
          }
          return(temp);
}



void concat(struct list *list1,struct list *list2)
{
          struct list *list3=NULL,*temp=NULL,*start=NULL;
          if(list1==NULL)
          {
                   if(list2==NULL)
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("BOTH LISTS ARE EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
                   else
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\nLIST 1 IS EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
          }
          else
          {
                   if(list2==NULL)
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\nLIST 2 IS EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
                   else
                   {
                             while (list1!=NULL)
                             {
                                      temp=(struct list*)malloc(sizeof(struct list));
                                      temp->info=list1->info;
                                      temp->link=NULL;
                                      if(list3==NULL)
                                                list3=start=temp;
                                      else
                                      {
                                                list3->link=temp;
                                                list3=list3->link;
                                      }
                                      list1=list1->link;
                                      temp=NULL;
                             }
                             while (list2!=NULL)
                             {
                                      temp=(struct list *)malloc(sizeof(struct list));
                                      temp->info=list2->info;
                                      temp->link=NULL;
                                      list3->link=temp;
                                      list3=list3->link;
                                      list2=list2->link;
                                      temp=NULL;
                             }

                   }
printf("\n\n~~~~~~~~~~~~~~~~~~~~ CONCATINATION ~~~~~~~~~~~~~~~~\n\n");
                   display(start);
          }
}


void intersection(struct list *list,struct list *list2)
{
          struct list *list3=NULL,*temp=NULL,*start=NULL,*list1=list;
          if(list1==NULL)
          {
                   if(list2==NULL)
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\nBOTH LISTS ARE EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
                   else
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\nLIST 1 IS EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
          }
          else
          {
                   if(list2==NULL)
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\nLIST 2 IS EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
                   else
                   {
                             while (list2!=NULL)
                             {
                                      list1=list;
                                      while (list1!=NULL)
                                      {
                                                if(list1->info==list2->info)
                                                {
                                                         temp=(struct list*)malloc(sizeof(struct list));
                                                          temp->info=list1->info;
                                                          temp->link=NULL;
                                                          if(list3==NULL)
                                                                   list3=start=temp;
                                                          else
                                                          {
                                                                   list3->link=temp;
                                                                   list3=list3->link;
                                                          }
                                                          temp=NULL;
                                                          break;
                                                }
                                                list1=list1->link;

                                      }
                                      list2=list2->link;
                             }
                   }
                   if(list3!=NULL)
                   {
printf("\n\n~~~~~~~~~~~~~~~~~~~~~ INTERSECTION ~~~~~~~~~~~~~~~~~~\n\n");
                             display(start);
                   }
                   else
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\nLIST 1 AND LIST 2 ARE DISTINCT LIST");
                             printf("\n\n---------------------------------------\n\n");
                   }
          }
 }

void uniion(struct list *list,struct list *list2)
{
          struct list *list3=NULL,*temp=NULL,*start=NULL,*list1=list;
          if(list1==NULL)
          {
                   if(list2==NULL)
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\nBOTH LIST ARE EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
                   else
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\nLIST 1 IS EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
          }
          else
          {
                   if(list2==NULL)
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\nLIST 2 IS EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
                   else
                   {
                             while (list1!=NULL)
                             {
                                      temp=(struct list*)malloc(sizeof(struct list));
                                      temp->info=list1->info;
                                      temp->link=NULL;
                                      if(list3==NULL)
                                                list3=start=temp;
                                      else
                                      {
                                                list3->link=temp;
                                                list3=list3->link;
                                      }
                                      list1=list1->link;
                                      temp=NULL;
                             }
                             while (list2!=NULL)
                             {
                                      list1=list;
                                      while (list1!=NULL)
                                      {
                                                if(list1->info==list2->info)
                                                          break;
                                                list1=list1->link;
                                      }
                                      if(list1==NULL)
                                      {
                                                temp=(struct list*)malloc(sizeof(struct list));
                                                temp->info=list2->info;
                                                temp->link=NULL;
                                                list3->link=temp;
                                                list3=list3->link;
                                      }
                                      temp=NULL;
                                      list2=list2->link;
                             }
                   }
                   if(list3!=NULL)
printf("\n\n~~~~~~~~~~~~~~~~~~ UNION ~~~~~~~~~~~~~~~~~~~~~\n\n");
                   display(start);
          }
}

void merge(struct list *list1,struct list *list2)
{
          struct list *list3,*temp=NULL,*start=NULL;
          if(list1==NULL)
          {
                   if(list2==NULL)
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\n BOTH LIST ARE EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
                   else
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\n LIST 1 IS EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
          }
          else
          {
                   if(list2==NULL)
                   {
                             printf("\n\n---------------------------------------\n\n");
                             printf("\n BOTH LIST ARE EMPTY");
                             printf("\n\n---------------------------------------\n\n");
                   }
                   else
                   {
                             list1=sort(list1);
                             list2=sort(list2);
                             list3=NULL;
                             while (list1!=NULL)
                             {
                                      temp=(struct list*)malloc(sizeof(struct list));
                                      temp->link=NULL;
                                      if(list2!=NULL)
                                      {
                                                if(list1->info<list2->info)
                                                {
                                                          temp->info=list1->info;
                                                          list1=list1->link;
                                                }
                                                else
                                                {
                                                          temp->info=list2->info;
                                                          list2=list2->link;
                                                }
                             }
                             else
                             {
                                      temp->info=list1->info;
                                      list1=list1->link;
                             }
                             if(list3==NULL)
                                      list3=start=temp;
                             else
                             {
                                      list3->link=temp;
                                      list3=list3->link;
                             }
                             temp=NULL;
                   }
                   while(list2!=NULL)
                   {
                             temp=(struct list*)malloc(sizeof(struct list));
                             temp->link=NULL;
                             temp->info=list2->info;
                             list2=list2->link;
                             list3->link=temp;
                             list3=list3->link;
                             temp=NULL;
                   }
printf("\n\n~~~~~~~~~~~~~~~~~~ MERGE ~~~~~~~~~~~~~~~~~~~~~\n\n");
                   display(start);
                   }
           }
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a Comment