双向循环链表的频度自学习算法

版权声明:版权归属tangobravo所有 https://blog.csdn.net/tangobravo/article/details/79296494

题目源自严奶奶的数据结构题集(c语言版) p19页的Algo2.38

ps:严奶奶的书默认都是带有头结点的。

设有一个双向循环链表,每个结点中除有prior,data,next三个域以外,还增设了一个访问频度域freq。在链表被启用之前,freq域的值均被初始化为零,而每当对链表进行一次Locate(L,x)的操作之后,被访问的结点(即元素值等于x的结点)中的频度域freq的值便增加1,同时调整链表中结点之间的次序,使其按照访问频度非递增的次序顺序排列,一遍始终保持被频繁访问的结点总是靠近表头结点。试编写符合上述要求的Locate(L,x)操作的算法。

思路:Locate到等于x的点,freq自增1,由于是递减排列,p指针向前寻找到第一个比x的freq大或者相等的结点,插在p后面

代码

头文件

//Status.h
#pragma once
#define TRUE 1
#define OK 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
//DuCycle_LinkList.h
#pragma once
#include <stdio.h>
#include "Status.h"
typedef int ElemType;
typedef struct DuCycle_Lnode {
	ElemType data;
	struct DuCycle_Lnode * next;
	struct DuCycle_Lnode * prior;
	int freq;
}DuCycle_Lnode,* DuCycle_List;
Status Init(DuCycle_List &L);
Status SelfLearning_Locate(DuCycle_List &L, ElemType e);//Algo2.38
Status Make_List(DuCycle_List &L);
Status Trav_DuCycle_List(DuCycle_List L);

函数文件

//fun.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "DuCycle_LinkList.h"
Status Init(DuCycle_List &L)
{
	L = (DuCycle_Lnode *)malloc(sizeof(DuCycle_Lnode));
	if (!L) exit(OVERFLOW);
	L->freq = 0;
	L->prior = L->next = NULL;
	L->data = NULL;
	return OK;
}//Init
Status Trav_DuCycle_List(DuCycle_List L)
{
	DuCycle_Lnode * p;
	p = L->next;
	if (L->next == NULL) return ERROR;
	while (p != L)
	{
		printf("%d(%d) ", p->data,p->freq);
		p = p->next;
	}
	/*putchar('\n');
	p = L->next;
	while (p != L)
	{
		printf("%d", p->freq);
		p = p->next;
	}*/
	putchar('\n');
	return OK;
}//Trav_DuCycle_List
Status SelfLearning_Locate(DuCycle_List &L,ElemType e)//Algo2.38
//自学习的Locate算法,结点中增加freq表示访问的频度
//每次访问将自组所有织结点重新按照freq降序排列
{
	DuCycle_Lnode *p, *q;
	p = NULL;
	q = L->next;
	while (q->next!=L&&q->data!=e)
	{
		q = q->next;
	}
	if (q->data == e)//发现e
	{
		q->freq++;
		p = q->prior;
		while (p!=L&&p->freq<q->freq)//移动p到第一个比e的freq大的结点上,或者头结点
		{
			p = p->prior;
		}
		q->next->prior = q->prior;
		q->prior->next = q->next;//ditach q
		q->next = p->next;//rejoin q 
		p->next->prior = q;
		p->next = q;
		q->prior = p;
		printf("element found.\n");
		return OK;
	}
	printf("element not found.\n");
	return ERROR;//未发现e
}//SelfLearning_Locate
Status Make_List(DuCycle_List &L)
{
	int n;
	printf("plz input the node you want to add.\n");
	scanf("%d", &n);
	printf("plz input %d numbers in.\n", n);
	for (int i = 0; i < n; i++)
	{
		ElemType e;
		scanf("%d", &e);
		DuCycle_Lnode *s= (DuCycle_Lnode *)malloc(sizeof(DuCycle_Lnode));
		if (!s) return ERROR;
		s->data = e;
		s->freq = 0;
		if (L->next != NULL)
		{
			s->next = L->next;
			L->next->prior = s;
			L->next = s;
			s->prior = L;
		}
		else
		{
			L->next = s;
			L->prior = s;
			s->prior = L;
			s->next = L;

		}
		
	}
	printf("List make succeed.\n");
	return OK;
}//Make_List

主函数

//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "DuCycle_LinkList.h"
#include "Status.h"
int main(void)
{
	DuCycle_List L;
	ElemType e;
	int n; 
	Init(L);
	Make_List(L);
	Trav_DuCycle_List(L);
	printf("input the element you want to locate.(not a number to quit)\n");
	while (scanf("%d",&e))
	{
		SelfLearning_Locate(L, e);
		Trav_DuCycle_List(L);
	}
	system("pause");
	return 0;
}

运行结果如下:

第一篇博客,写的不好还请见谅。如果有错误的地方欢迎留言指出 :)





猜你喜欢

转载自blog.csdn.net/tangobravo/article/details/79296494