不同于市面上的用c++写的noj,此文章由c写,符合在校生需求,且附有注释,且看可可我慢慢更新。
相信这周很多同学已经做完第6题了,可可做慢了,私密马赛。
思路:找到要插入的位置和要插入的数据,先删除原数据再插入。通过对pre进行比较来找到要插入的位置。
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char data;
int freq;
struct node* next, * pre;
}Lnode,*Linklist;
void CREAT(Linklist head,int DNodeCount)//创建双向循环链表 //未注明返回类型
{
char temp;
int i;
Linklist tail;
tail = head;
for (i = 0; i < DNodeCount; i++)
{
Linklist p = (Linklist)malloc(sizeof(Lnode)); //使用了 new关键字 作为变量名
scanf(" %c", &temp);//因为是字符型,所以要排除空格,在%c前加个空格
p->data = temp;
p->freq = 0;
p->pre = tail;
tail->next = p;
tail = p;
}
tail->next = head;
head->pre = tail;
}
void LOCATE(Linklist head,char accessData) //未注明返回类型
{
Linklist temp = head->next;
Linklist p, access;
p = head->next;
while (temp!= head)//遍历链表
{
while (temp->data != accessData)//temp的data不是所找就向后
{
temp = temp->next;
}
access = temp;//找到了
access->freq++;
while (p->freq >= temp->freq)//找到该插入的位置
{
p = p->next;
}
access->next->pre = access->pre;
temp->pre->next = temp->next;
temp = temp->next;
access->pre = p->pre;
access->next = p;
p->pre->next = access;
p->pre = access;
break;
}
}
int main()
{
int accessCount, DNodeCount;
int i;
char accessData;
Linklist head;
head= (Linklist)malloc(sizeof(Lnode));
head->next = NULL;
head->freq = 0;
head->pre = NULL;
scanf("%d %d", &DNodeCount, &accessCount);
CREAT(head, DNodeCount);
for (i = 0; i < accessCount; i++)
{
scanf(" %c", &accessData);
LOCATE(head, accessData);
}
Linklist temp = head->next;
for (i = 0; i < DNodeCount; i++)
{
printf("%c ", temp->data);
temp = temp->next;
}
return 0;
}