2.實現下述要求的Locate運算的函數
問題描述
設有一個帶表頭結點的雙向鏈表L,每個結點有4個數據成員:指向前驅結點的指針prior、指向后繼結點的指針next、存放數據的成員data和訪問頻度freq。所有結點的freq初始時都為0。每當在鏈表上進行一次Locate?(L,?x)操作時,令元素值為x的結點的訪問頻度freq加1,并將該結點前移,鏈接到與它的訪問頻度相等的結點后面(如果該結點沒有找到與它訪問頻度相等的結點,鏈接到表頭后面結點),使得鏈表中所有結點,保持按訪問頻度遞減的順序排列,以使頻繁訪問的結點總是靠近表頭。
?
解決方案要求
輸入參數:
- 輸入n,n表示雙向鏈表L的長度,L中的結點的data域依次為1到n。
- 隨機多次調用Locate函數,輸入x(調用函數次數由用戶決定)。
輸出參數:
調用Locate函數結束后,從頭結點開始依次輸出鏈表L中個結點的內容(data+freq)。
參考樣例:
?
?
代碼:
1 #include "stdlib.h" 2 #include <iostream> 3 using namespace std; 4 5 typedef int ListData; 6 typedef struct DoubleNode { 7 ListData data, frequency; 8 struct DoubleNode *prior, *next; 9 }DoubleNode, *DoubleList; 10 11 void CreateDoubleList (DoubleList &first) { 12 first = (DoubleNode*)malloc(sizeof(DoubleNode)); 13 if(first == NULL) { 14 cout << "存儲分配錯誤!" << endl; 15 exit(1); 16 } 17 first->prior = first->next = first; 18 } 19 20 void IniteList(DoubleList &first, ListData n) { 21 DoubleNode *carrier, *temp = first; 22 for (int i = 0; i < n; i++) { 23 carrier = (DoubleNode*)malloc(sizeof(DoubleNode)); 24 carrier->data = i + 1; 25 carrier->frequency = 0; 26 //Insert 27 carrier->prior = temp; 28 temp->next = carrier; 29 carrier->next = first; 30 carrier->next->prior = carrier; 31 32 temp = carrier; 33 } 34 } 35 36 DoubleList Locate(DoubleList &first, ListData x, ListData n) { 37 DoubleNode *temp = first->next; 38 while(temp->data != x && temp != first) { 39 temp = temp->next; 40 } 41 if (temp == first) { 42 cout << "Please input number range from " << 1 << " to " << n << endl; 43 exit(1); 44 } 45 else 46 return temp; 47 } 48 49 void SortList(DoubleList &first, ListData x, ListData n) { 50 DoubleNode *temp; 51 temp = Locate(first, x, n); 52 int tempNumber; 53 temp->frequency++; 54 if (temp->prior != first) { 55 while (temp->frequency > temp->prior->frequency) { 56 //SWAP 57 tempNumber = temp->prior->data; 58 temp->prior->data = temp->data; 59 temp->data = tempNumber; 60 //cout << "temp->prior->data " << temp->prior->data << endl; 61 //cout << "temp->data " << temp->data << endl; 62 63 tempNumber = temp->prior->frequency; 64 temp->prior->frequency = temp->frequency; 65 temp->frequency = tempNumber; 66 //cout << "temp->prior->frequency " << temp->prior->frequency << endl; 67 //cout << "temp->frequency " << temp->frequency << endl; 68 } 69 } 70 } 71 72 int main() { 73 int n; 74 cout << "Please input the link list length:" << endl; 75 cin >> n; 76 77 cout << "The link list data are" << endl; 78 for (int i = 0; i < n; i++) 79 cout << "The " << i + 1 << " node is " << i + 1 << ", its frequency is 0." << endl; 80 81 cout << "Let's start to test Locate Function.(-1 meansstopping input number)" << endl; 82 83 DoubleList first; 84 CreateDoubleList(first); 85 IniteList(first, n); 86 87 int x = 0; 88 while(1) { 89 cout << "Please input number :"; 90 cin >> x; 91 if (x != -1) { 92 SortList(first, x, n); 93 } 94 else break; 95 96 97 } 98 cout << "After test, the link list data are:" << endl; 99 DoubleNode *temp = first->next; 100 int count = 0; 101 while(temp != first) { 102 count++; 103 cout << "The " << count << " node is " << temp->data 104 << ", its frequency is " << temp->frequency << "." << endl; 105 temp = temp->next; 106 } 107 108 return 0; 109 }
?