#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
struct node
{
char data[10];
int value;//输入频率
struct node *next;
};
int main()
{
struct node *p,*q,*t,*h;
p = (struct node*)malloc(sizeof(struct node));
h = (struct node*)malloc(sizeof(struct node));
t = (struct node*)malloc(sizeof(struct node));
q = (struct node*)malloc(sizeof(struct node));
h->next = p;
strcpy(p->data,"的");
p->value = 4;
p->next = q;
p = p->next;
strcpy(p->data,"地");
p->value = 3;
q = (struct node*)malloc(sizeof(struct node));
p->next = q;
p = p->next;
strcpy(p->data,"第");
p->value = 2;
q = (struct node*)malloc(sizeof(struct node));
p->next = q;
p = p->next;
strcpy(p->data,"低");
p->value = 1;
q = (struct node*)malloc(sizeof(struct node));
p->next = q;
p = p->next;
strcpy(p->data,"底");
p->value = 0;
p->next = NULL;
//以上为初始化
p = h->next;
char a[10];
printf("请输入汉字:(输入break结束)\n");
while(1)
{
scanf("%s",a);
if(strcmp(a,"break") == 0)
break;
while(1)
{
if(strcmp(a,p->data)==0)
{
p->value = p->value+1;
break;
}
else{
if(p->next == NULL)
{
printf("输入错误!!\n");
return 0;
}
p = p->next;
}
}
p = h->next;
q = p->next;
while(q->next != NULL)
{
if(p->value <= q->value)//根据频率交换
{
strcpy(t->data,p->data);
strcpy(p->data,q->data);
strcpy(q->data,t->data);
(t->value= p->value);
(p->value= q->value);
(q->value= t->value);
}
p = q;
q = q->next;
}
if(p->value <= q->value)//此时q->next = NULL;再单独进行一次比较,确定是否交换
{
strcpy(t->data,p->data);
strcpy(p->data,q->data);
strcpy(q->data,t->data);
(t->value= p->value);
(p->value= q->value);
(q->value= t->value);
}
p = h->next;
while(p->next !=NULL)
{
printf("%s ",p->data);
printf("%d ",p->value);
p = p->next;
}
printf("%s",p->data);
printf("%d\n",p->value);
p = h->next;
}
return 0;
}