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 }
时间: 2024-12-29 06:45:43