Created
March 9, 2019 16:15
-
-
Save luk0y/cd16abdeccaa0a8227f2648503c6d366 to your computer and use it in GitHub Desktop.
Finding the frequency of the elements in the single linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//By luk0y | |
#include<stdio.h> | |
#include<stdlib.h> | |
struct node | |
{ | |
int data; | |
struct node *adr; | |
}; | |
void main() | |
{ | |
struct node *start=NULL,*nn,*ptr,*pttr; | |
int x,y,n=0,l=0,i,j=0; | |
int count=0; | |
printf("Enter the nodes you want : \n"); | |
scanf("%d",&y); | |
int k=y; | |
printf("Enter the values :\n"); | |
while(y!=0) | |
{ | |
scanf("%d",&x); | |
nn=(struct node*) malloc(sizeof(struct node)); | |
if(nn==NULL) | |
{ | |
printf("no space"); | |
} | |
else | |
{ | |
n=n+1; | |
if(start==NULL) | |
{ | |
start=nn; | |
nn->data=x; | |
nn->adr=NULL; | |
} | |
else | |
{ | |
ptr=start; | |
while(ptr->adr!=NULL) | |
{ | |
ptr=ptr->adr; | |
} | |
ptr->adr=nn; | |
nn->data=x; | |
nn->adr=NULL; | |
} | |
} | |
y--; | |
} | |
ptr=start; | |
printf("Linked list : \n"); | |
while(ptr!=NULL) | |
{ | |
printf("%d\n",ptr->data); | |
ptr=ptr->adr; | |
} | |
printf("\n\n"); | |
for(i=1;i<n;i++) | |
{ | |
ptr=start; | |
pttr=ptr->adr; | |
for(j=0;j<(n-i);j++) | |
{ | |
if(ptr->data>pttr->data) | |
{ | |
l=ptr->data; | |
ptr->data=pttr->data; | |
pttr->data=l; | |
} | |
ptr=pttr; | |
pttr=pttr->adr; | |
} | |
} | |
ptr=start; | |
printf("Final : \n"); | |
x=ptr->data; | |
while(ptr!=NULL) | |
{ | |
if(ptr->data==x){ | |
count=count+1; | |
ptr=ptr->adr; | |
} | |
else | |
{ | |
printf("Frequency of %d = %d\n",x,count); | |
count=0; | |
x=ptr->data; | |
} | |
} | |
printf("Frequency of %d = %d\n",x,count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment