All pastes #39955 Raw Edit

Anonymous

public text v1 · immutable
#39955 ·published 2006-02-04 14:16 UTC
rendered paste body
struct entry
{
	struct entry * next;
	char *str;
	int count;
};

struct entry * head=NULL, *tail=NULL;

int function(char * filename)
{
	FILE *fp;
	int ch='\n';char str[100];
	int count,flag;
	struct entry * temp,* temp1;
	
	int tempcount;
	char tempstr[100];
	fp=fopen(filename,"r");
	
	while(ch!=EOF)
	{
		count=0;
		ch=fgetc(fp);
		
		while(ch!='\n' && ch!=EOF)
		{
			str[count]=ch;
			count++;
			ch=fgetc(fp);
		}
		
		str[count]='\0';
		if(ch==EOF)
			continue;
			
		temp=(struct entry *)(malloc(sizeof(struct entry)));
		
		//copy contents
		temp->str=(char *)(malloc(strlen(str)+1));
		strcpy(temp->str,str);
		temp->count=1;
		temp->next=NULL;
		
		if(head==NULL)
		{
			head=temp;
			tail=temp;
		}
		else
		{
			temp1=head;
			flag=0;

			while(temp1!=NULL)
			{	
				if(strcmp(str,temp1->str)==0)
				{
					temp1->count++;
					flag=1;
					break;
				}
				else
				{	
					temp1=temp1->next;
					continue;
				}
			}
			
			if(flag==0)
			{
				tail->next=temp;
				tail=temp;
			}
		}
	}
	
	temp=head;
	while(temp!=NULL)
	{
		temp1=temp->next;
		while(temp1!=NULL)
		{
			if(temp->count<temp1->count)
			{
				tempcount=temp->count;
				temp->count=temp1->count;
				temp1->count=tempcount;
				
				strcpy(tempstr,temp->str);
				strcpy(temp->str," ");
				strcpy(temp->str,temp1->str);	
				strcpy(temp1->str," ");
				strcpy(temp1->str,tempstr);
			}
			temp1=temp1->next;
		}
		temp=temp->next;
	}
	
	temp=head;
	count=0;
	while(temp!=NULL && count<5)
	{
		printf("%s %d\n",temp->str,temp->count);
		temp=temp->next;
		count++;
	}
}