Description:
用链表A存储一组字符串([1, 80]),建立一个链表B,存储其中ASCLL码字符最小的数出现的次数等于n的字符串。
#include<>
#include<>
#include<>
struct N
{
char s[85]; //一开始定义成了char *s,下接[1];
struct N *next;
int t; //t用来记录s中ascll码最小的数出现的次数
};
int t(char *s) //计算s中ascll码最小的数出现的次数
{
int len = strlen(s);
char min = s[0];
int t = 1;
for(int i=1; i<=len-1; i++)
{
if(s[i]<min)
{
t=1;
min = s[i];
}
else if(s[i]==min) t++;
}
return t;
}
struct N *createA(int num)
{
struct N *headA = (struct N*)malloc(sizeof(struct N));
struct N *end = headA;
for(int i=0; i<num; i++)
{
struct N *tmp = (struct N*)malloc(sizeof(struct N));
scanf("%s", tmp->s); //上接[1],对于char*s,没有办法正常循环输入,因为还未分配内存
tmp->t = t(tmp->s);
end->next = tmp;
end = tmp;
}
end->next = NULL;
return headA;
}
struct N *createB(struct N* headA, int n)
{
struct N *headB = (struct N*)malloc(sizeof(struct N));
struct N *endB = headB;
struct N *p = headA->next;
while(p!=NULL)
{
if(p->t == n)
{
endB->next = p;
endB = p;
}
p = p->next;
}
endB->next = NULL;
return headB;
}
int main()
{
int num;
scanf("%d", &num);
struct N *headA = createA(num);
int n;
scanf("%d", &n);
struct N *headB = createB(headA, n);
struct N *p = headB->next;
while(p!=NULL)
{
printf("%s ", p->s);
p = p->next;
}
return 0;
}