C program to find the Number of Occurrences of all the elements present in the Linked list

PROGRAM:

/*list refers to original linked list and occur list refers to the list which is used to store the elements and its corresponding occurrences*/
#include<stdio.h>
struct node
 {
  int data;
  struct node *next;
 } *head,*temp,*temp1,*first,*last;
/*Structure occur is to store the element(in data) and its corresponding occurrences (in count) and next is to store next occur nodes address*/
struct occur
 {
   int data;
   int count;
   struct occur *next;
 } *head1,*occur1,*newoccur;
void create();
void print();
void occurrence();
void printOccur();
void main()
 {
   create();
   printf("\nElements in the entered list:\n");
   print();
   occurrence();
   printf("\nElements and their corresponding occurrences:\n");

   printf("Element\tOccurrences:\n\n");
   printOccur();
 }
void create()
 {
   int num;
   printf("\nEnter element or enter -1 to exit\n");
   head=(struct node*)malloc(sizeof(struct node));
   scanf("%d",&num);
   if(num==-1)
     {
      head=NULL;
     }
   else
     {
       head->data=num;
       head->next=NULL;
     }
   while(head!=NULL)
     {
       printf("\nEnter element or enter -1 to exit\n");
       scanf("%d",&num);
       if(num==-1)
{break;}
       else
{
 temp=head;
 while(temp->next!=NULL)
   {
     temp=temp->next;
   }
 temp1=(struct node*)malloc(sizeof(struct node));
 temp1->data=num;
 temp1->next=NULL;
 temp->next=temp1;
}
     }
 }
/*occurrence function calculates the number of occurrences of the elements.*/
void occurrence ()
 {
   int found;
   /*first element in occur list is created and it is named as head1*/
   head1=(struct occur*)malloc(sizeof(struct occur));
   head1->data=head->data;
   head1->count=0;
   head1->next=NULL;
     for(temp=head;temp!=NULL;temp=temp->next)
       {
found=0;
for(occur1=head1;occur1->next!=NULL;occur1=occur1->next)
 {
            /*if the element in the list is found in the occur list then its count value is incremented by 1.*/
   if(occur1->data==temp->data)
    {
     found=1;
     occur1->count++;
    }
 }
          /*if the element is not found in the occur list then new node is created for that element and it is placed at the starting of the occur list(to minimize complexity)*/
 if(found==0)
  {
   newoccur=(struct occur *)malloc(sizeof(struct occur));
   newoccur->data=temp->data;
   newoccur->count=1;
   newoccur->next=head1;
   head1=newoccur;
           }
       }
 }
void print()
 {
   struct node *head1=head;
   while(head1!=NULL)
    {
     printf("%d\t",head1->data);
     head1=head1->next;
    }
 }
/*printOccur function is to print the elements and corresponding occurrences in the occur list*/
void printOccur()
 {
   struct occur *temp=head1;
   while(temp->next!=NULL)
    {
     printf("%4d%8d\n",temp->data,temp->count);
     temp=temp->next;
    }
 }
/*In occur list new node is inserted at starting just for reducing complexity....*/
OUTPUT:


ELEMENTS-OCCURANCES-IN-LINKED-LIST






Frenzz try this one with different inputs.....


Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment