Locete操作
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
struct Node *pre;
char data;
struct Node *next;
int freq;
}Node,*ListNode;
void CreatList(ListNode name,int n)
{
ListNode temp=name;
for(int i=0;i<n;i++)
{
ListNode p=(ListNode)malloc(sizeof(Node));
p->data=0;
p->freq=0;
p->pre=temp;
temp->next=p;
temp=temp->next;
}
temp->next=name;
}
void paixu(ListNode name,int n)
{
ListNode p=name->next;
ListNode q=p->next;
char k;
int f;
for(int i=0;i<n-1;i++)
{
p=name->next;
q=p->next;
for(int j=0;j<n-i-1;j++)
{
if(p->freq < q->freq)
{
k=p->data;
p->data=q->data;
q->data=k;
f=p->freq;
p->freq=q->freq;
q->freq=f;
}
p=p->next;
q=q->next;
}
}
}
void PrintList(ListNode name)
{
ListNode p =name->next;
while(p!=name)
{
printf("%c ",p->data);
p=p->next;
}
}
void fangwen(ListNode name,char a)
{
ListNode p=name->next;
while(p!=name)
{
if(p->data == a)
{
p->freq++;
break;
}
else
{
p=p->next;
}
}
}
int main()
{
int m,n;
scanf("%d %d",&m,&n);
ListNode a=(ListNode)malloc(sizeof(Node));
CreatList(a,m);
ListNode temp=a->next;
int i=0;
while(1)
{
char c=getchar();
if(c!=' '&&c!='\n')
{
temp->data=c;
temp=temp->next;
i++;
if(i==m)
{
break;
}
}
}
ListNode b=(ListNode)malloc(sizeof(Node));
CreatList(b,n);
ListNode tempb=b->next;
// char nouse2=getchar();
int j=0;
while(1)
{
char c=getchar();
if(c!=' '&&c!='\n')
{
tempb->data=c;
tempb=tempb->next;
j++;
if(j==n)
{
break;
}
}
}
ListNode bb=b->next;
while(bb!=b)
{
fangwen(a,bb->data);
bb=bb->next;
}
paixu(a,m);
PrintList(a);
return 0;
}