## 問題描述
> 設頭指針為L的帶有表頭結點的非循環雙向鏈表,其每個結點中除有prior(前驅指針),data(數據),next(后繼指針)域外,還有一個訪問頻度域freq。在鏈表被啟用前,其值均初始化為零。每當在連表中進行一次Locate(L,x)運算時,令元素值為x的結點中freq域的值增1,并使此鏈表中結點保持按訪問頻度非遞增的順序排列,同時最近訪問的結點排在頻度相同的結點的前面,以便使頻繁訪問的結點總是靠近表頭。試編寫符合上述要求的Locate(L,x)運算的算法,該運算為函數過程,返回找到結點的地址,類型為指針型。
## 算法思想
> 本題經過分析之后可以發現,主要考察了雙鏈表的查找,刪除,插入操作。
> - 首先在雙鏈表中查找數據域的值為x的結點;
> - 找到后,將結點從鏈表上摘下,然后再從頭節點按照頻度遞減的次序向后查找,插入到同頻度出現的第一個位置
> - 這樣便完成了一次排序
## 算法描述
~~~
void Locate(DNode *head, ElemType x)
{
DNode *pre=head;
DNode *p=head->next;
while(p&&p->data!=x){
p=p->next;
}
if(!p){
printf("The %d isn't exist!\n",x);
return;
}else{
p->freq++;
if(p->next){
p->next->prior=p->prior;
}
p->prior->next=p->next;
pre=p->prior;
while(pre!=head&&pre->freq<p->freq){
pre=pre->prior;
}
p->next=pre->next;
pre->next->prior=p;
p->prior=pre;
pre->next=p;
}
}
~~~
具體代碼見附件。
## 附件
~~~
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct DNode{
int freq;
ElemType data;
struct DNode *prior,*next;
}DNode,*DLinkList;
DLinkList CreatList(DNode*);
void Locate(DNode*,ElemType);
void BackPrint(DNode*);
int main(int argc,char* argv[])
{
DNode *head;
head=(DNode*)malloc(sizeof(DNode));
head->prior=NULL;
head->next=NULL;
head=CreatList(head);
BackPrint(head);
//模擬結點數據域的值出現的次數
for(ElemType x=1;x<=8;x++){
if(x%2==0){
Locate(head,x);
if(x%3==0){
Locate(head,x);
}
}
}
BackPrint(head);
return 0;
}
//頭插法建立非循環雙向鏈表
DLinkList CreatList(DNode *head)
{
DNode *L;
int freq=0;
ElemType x;
scanf("%d",&x);
while(x!=999){
L=(DNode*)malloc(sizeof(DNode));
L->freq=freq;
L->data=x;
L->next=head->next;
if(head->next){
head->next->prior=L;
}
L->prior=head;
head->next=L;
scanf("%d",&x);
}
return head;
}
void Locate(DNode *head, ElemType x)
{
DNode *pre=head;
DNode *p=head->next;
while(p&&p->data!=x){
p=p->next;
}
if(!p){
printf("The %d isn't exist!\n",x);
return;
}else{
p->freq++;
if(p->next){
p->next->prior=p->prior;
}
p->prior->next=p->next;
pre=p->prior;
while(pre!=head&&pre->freq<p->freq){
pre=pre->prior;
}
p->next=pre->next;
pre->next->prior=p;
p->prior=pre;
pre->next=p;
}
}
void BackPrint(DNode *head)
{
DNode *p=head;
while(p->next){
p=p->next;
printf("%4d|%d",p->data,p->freq);
}
printf("\n");
}
~~~
- 前言
- 緒論
- 第1章線性表
- 第1章第1節 線性表的順序表示
- 第1章第1節練習題1 刪除最小值
- 第1章第1節練習題2 逆置順序表
- 第1章第1節練習題3 刪除指定元素
- 第1章第1節練習題4 有序表刪除指定區間值
- 第1章第1節練習題5 無序表刪除指定區間值
- 第1章第1節練習題6 刪除重復值
- 第1章第1節練習題7 順序表的歸并
- 第1章第1節練習題8 順序表循環移位
- 第1章第1節練習題9 查找指定值
- 第1章第1節練習題10 查找中位數
- 第1章第2節 線性表的鏈式表示(1)
- 第1章第2節 線性表的鏈式表示(2)
- 第1章第2節 線性表的鏈式表示(3)
- 第1章第2節練習題1 遞歸刪除指定結點
- 第1章第2節練習題2 非遞歸刪除指定結點
- 第1章第2節練習題3 刪除最小值結點
- 第1章第2節練習題4 刪除指定區間結點
- 第1章第2節練習題5 刪除重復結點
- 第1章第2節練習題6 反向輸出
- 第1章第2節練習題7 遞減輸出
- 第1章第2節練習題8 奇偶拆分單鏈表
- 第1章第2節練習題9 查找公共結點
- 第1章第2節練習題10 查找指定倒數結點
- 第1章第2節練習題11 就地逆置單鏈表
- 第1章第2節練習題12 單鏈表之插入排序
- 第1章第2節練習題13 單鏈表之選擇排序
- 第1章第2節練習題14 判斷子序列
- 第1章第2節練習題15 拆分并逆序單鏈表
- 第1章第2節練習題16 歸并并逆序單鏈表
- 第1章第2節練習題17 使用相同值結形成新單鏈表
- 第1章第2節練習題18 求兩個單鏈表的交集
- 第1章第2節練習題19 判斷循環雙鏈表對稱
- 第1章第2節練習題20 連接兩個循環單鏈表
- 第1章第2節練習題21 輸出并刪除最小值結點
- 第1章第2節練習題22 按結點訪問頻度排序
- 第1章第3節 線性表的比較
- 第2章受限的線性表
- 第2章第1節 棧
- 第2章第1節練習題1 判斷棧的操作次序是否合法
- 第2章第1節練習題2 判斷是否中心對稱
- 第2章第2節 隊列
- 第2章第1節練習題3 共享棧的基本操作
- 第2章第2節練習題1 逆置隊列
- 第2章第2節練習題2 使用棧模擬隊列操作
- 第2章第2節練習題3 使用隊列模擬渡口管理
- 第2章第3節 串
- 第2章第3節練習題1 串的模式匹配(Basic)
- 第2章第3節練習題2 串的模式匹配(KMP)