/********************************************************************************
* @File name: Ex2.20
* @Author: DuChenyang
* @Version: 1.1
* @Date: 2022-09-12
* @Description: Double circular linked list
**Attention:
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
//创建结构体
typedef struct node {
int data;
int freq;
struct node * prior;
struct node * next;
}Node;
//创建链表
Node * createList(int n) {
//A local variable
int i = 0;
//初始化指针
Node * head = NULL, *loopNode = NULL;
//给第一个节点分配地址空间
head = (Node*)malloc(sizeof(Node));
//链表中的第一个节点
//printf("输入第1个元素的值:\n");
//scanf("%d",&head->data);
head->data = 0;
//initialize the other arguments in the head struct.
head->freq =NULL;
head->next = NULL;
head->prior = NULL;
loopNode = head;
//用for循环将其他节点添加到链表
//将节点加到链表的尾部
for (i = 2; i <= n; i++) {
//给其他节点分配地址
Node * body = (Node*)malloc(sizeof(Node));
//初始化
body->prior = NULL;
body->next = NULL;
//设置其他元素的值和访问频度
printf("输入第%d元素的值:\n",i);
scanf("%d",&body->data);
body->freq = 0;
//建立双向链表
loopNode->next = body;
body->prior = loopNode;
//移动指针位置
loopNode = loopNode->next;
}
loopNode->next = head;//首尾相连
head->prior = loopNode;
return head;
}
Node * locate(Node * head, int x){
Node * pTemp = head;
while(pTemp->data != x){
pTemp = pTemp -> next;
printf("测试循环:pTemp->data=%d %d",pTemp->data,pTemp->freq);
}
pTemp->freq++;
pTemp->prior->next = pTemp->next;
pTemp->prior = pTemp ->next->prior; //从链表上将pTemp结点摘下来
printf("测试摘除:pTemp->data=%d %d",pTemp->data,pTemp->freq);
/***
打印删除完成的链表
*/
Node * node = head->next;
printf("\n删除完节点的链表\n");
while(node != head){
printf("data=%d freq=%d\n",node->data,node->freq);
node = node->next;
}
/**
寻找待插入节点的前驱节点::::::
*/
Node *p = head->next;
if(p->freq <= pTemp->freq){
printf("当前频率比较p=%d pTemp=%d\n",p->freq,pTemp->freq);
p = p->prior;
printf("调整后频率比较p=%d pTemp=%d\n",p->freq,pTemp->freq);
}
else{
//
while (p != head && p->freq > pTemp->freq) {
printf("j = 测试位置》——《");
p = p->next;//利用循环找到待插入节点的前驱
}
}
pTemp->next = p->next;//将带插入节点的后继指向当前节点的后继
p->next->prior = pTemp;//将当前插入节点的后继的前驱指向待插入节点
p->next = pTemp;//当前节点的后继指向待插入节点
pTemp->prior = p;//待插入节点的前继指向当前节点
return head;
}
void printList(Node *head, int count){
Node * node = head->next;
printf("\n打印次数%d\n",count);
while(node != head){
printf("data=%d freq=%d\n",node->data,node->freq);
node = node->next;
}
}
int main() {
int n = 0;
int findVal;
Node * head = NULL;
printf("输入链表内的元素个数:");
scanf("%d",&n);
//Call the fuction to create a loop list
head = createList(n);
printList(head,1);
while(findVal != -1){
printf("输入要访问的元素");
scanf("%d",&findVal);
head = locate(head,findVal);
printList(head,1);
}
printList(head,2);
return 0;
}
上述代码总会出现随机弄丢节点的问题。
/********************************************************************************
* @File name: Ex2.20
* @Author: DuChenyang
* @Version: 1.1
* @Date: 2022-09-12
* @Description: Double circular linked list
**Attention:
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
//创建结构体
typedef struct node {
int data;
int freq;
struct node * prior;
struct node * next;
}Node;
//创建链表
Node * createList(int n) {
//A local variable
int i = 0;
//初始化指针
Node * head = NULL, *loopNode = NULL;
//给第一个节点分配地址空间
head = (Node*)malloc(sizeof(Node));
//链表中的第一个节点
//printf("输入第1个元素的值:\n");
//scanf("%d",&head->data);
head->data = 0;
//initialize the other arguments in the head struct.
head->freq =NULL;
head->next = NULL;
head->prior = NULL;
loopNode = head;
//用for循环将其他节点添加到链表
//将节点加到链表的尾部
for (i = 2; i <= n; i++) {
//给其他节点分配地址
Node * body = (Node*)malloc(sizeof(Node));
//初始化
body->prior = NULL;
body->next = NULL;
//设置其他元素的值和访问频度
printf("输入第%d元素的值:\n",i);
scanf("%d",&body->data);
body->freq = 0;
//建立双向链表
loopNode->next = body;
body->prior = loopNode;
//移动指针位置
loopNode = loopNode->next;
}
loopNode->next = head;//首尾相连
head->prior = loopNode;
return head;
}
Node * locate(Node * head, int x){
Node * pTemp = head;
while(pTemp->data != x){
pTemp = pTemp -> next;
printf("测试循环:pTemp->data=%d %d",pTemp->data,pTemp->freq);
}
Node *pTempNext = pTemp->next;
Node *pTempPrior = pTemp->prior;
printf("上一个%d\n",pTempPrior->data);
printf("下一个%d\n",pTempNext->data);
pTemp->freq++;
//pTemp->prior->next = pTemp->next;
//pTemp->prior = pTemp ->next->prior; //从链表上将pTemp结点摘下来
pTempPrior->next = pTempNext;
pTempNext -> prior = pTempPrior;
printf("测试摘除:pTemp->data=%d %d",pTemp->data,pTemp->freq);
/***
打印删除完成的链表
*/
Node * node = head->next;
printf("\n删除完节点的链表\n");
while(node != head){
printf("data=%d freq=%d\n",node->data,node->freq);
node = node->next;
}
/**
寻找待插入节点的前驱节点::::::
*/
Node *p = head->next;
if(p->freq <= pTemp->freq){
printf("当前频率比较p=%d pTemp=%d\n",p->freq,pTemp->freq);
p = p->prior;
printf("调整后频率比较p=%d pTemp=%d\n",p->freq,pTemp->freq);
}
else{
//
while (p != head && p->freq > pTemp->freq) {
printf("j = 测试位置》——《");
p = p->next;//利用循环找到待插入节点的前驱
}
}
pTemp->next = p->next;//将带插入节点的后继指向当前节点的后继
p->next->prior = pTemp;//将当前插入节点的后继的前驱指向待插入节点
p->next = pTemp;//当前节点的后继指向待插入节点
pTemp->prior = p;//待插入节点的前继指向当前节点
return head;
}
void printList(Node *head, int count){
Node * node = head->next;
printf("\n打印次数%d\n",count);
while(node != head){
printf("data=%d freq=%d\n",node->data,node->freq);
node = node->next;
}
}
int main() {
int n = 0;
int findVal;
Node * head = NULL;
printf("输入链表内的元素个数:");
scanf("%d",&n);
//Call the fuction to create a loop list
head = createList(n);
printList(head,1);
while(findVal != -1){
printf("输入要访问的元素");
scanf("%d",&findVal);
head = locate(head,findVal);
printList(head,1);
}
printList(head,2);
return 0;
}
改进如上所述,把摘取节点的前驱节点和后继节点分别保存,就不会出现随机弄丢节点的情况了。
提示:以上代码还有头节点处理bug、频度访问排序bug,本人还在继续改正中~~初学者,仅记录自己的学习