#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAX 50
typedef struct str
{
char ch;
int ccount;
struct str *next;
}node;
node *func(char a[],int n);
int main()
{
int index=0;
node *head,*head2,*temp;
char s[MAX],res[MAX]; //最大输入字符可以自己设置
scanf("%s",s);
head2=head=func(s,sizeof(s));
while(head)
{
res[index++]=head->ch;
res[index++]=head->ccount+48;
head=head->next;
}
res[index]='\0';
printf("\nres=%s",res);
while(head2)
{
temp=head2->next;
free(head2);
head2=temp;
}
return 0;
}
node *func(char a[],int n) //把结果放在链表中,返回链表头
{
int i;
node *temp,*head,*head2,*head3;
bool new;
for(i=0;i<n;i++)
{
if(i==0)
{
temp=(node *)malloc(sizeof(node));
temp->ch=a[i];
temp->ccount=0;
temp->next=NULL;
head=temp;
head3=head2=temp;
}
while(head3) //head3为了方便重置投指针
{
if(a[i]==head3->ch)
{
head3->ccount++;
new=false;
break;
}
head3=head3->next;
new=true;
}
head3=head;
if(new) //head2指向最后一个链表
{
temp=(node *)malloc(sizeof(node));
temp->ch=a[i];
temp->ccount=1;
temp->next=NULL;
if((head2->next)!=NULL)
head2=head2->next;
head2->next=temp;
}
}
return head;
}
温馨提示:答案为网友推荐,仅供参考