//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
struct node{
char w[21];
int n;
struct node *next;
};
void freenode(struct node *p){
if(p->next) freenode(p->next);
free(p);
}
struct node *CreateLink(int n){
struct node *head,*p,*ptmp;
if((p=head=(struct node *)malloc(sizeof(struct node)))==NULL){
printf("Establish a head node failure...");
return NULL;
}
while(n--){
if((p->next=(struct node *)malloc(sizeof(struct node)))==NULL){
printf("Failed to create node...");
freenode(head);
return NULL;
}
scanf("%s",p->next->w);
for(ptmp=head->next;ptmp!=p->next;ptmp=ptmp->next)
if(strcmp(ptmp->w,p->next->w)==0){
ptmp->n++;
delete p->next;
break;
}
if(ptmp==p->next){
p=p->next;
p->n=1;
}
}
p->next=NULL;
p=head;
head=head->next;
free(p);
return head;
}
int main(void){
int n,k;
struct node *head,*p,*q,*t,node;
while(1){
printf("How many words want to input?\nn=");
if(scanf("%d",&n),n>0)
break;
printf("Error, redo: ");
}
printf("Input some words...\n");
if(head=CreateLink(n)){
printf("Output several?\nk=");
if(scanf("%d",&k),k<0)
k=n;
for(n=0,p=head;p;p=p->next){
for(t=p,q=p->next;q;q=q->next)
if(t->n < q->n)
t=q;
if(p!=t){
strcpy(node.w,p->w);
strcpy(p->w,t->w);
strcpy(t->w,node.w);
node.n=p->n;
p->n=t->n;
t->n=node.n;
}
printf("%s ",p->w);
if(n++>k) break;
}
freenode(head);
printf("\n");
}
else printf("Failed to create the list...\n");
return 0;
}