LRU缓存结构【C语言】

c 复制代码
#include <stdio.h>
#include <stdlib.h>

//双链表节点结构
typedef struct Node {
	int key;
	int value;
	struct Node* pre;
	struct Node* next;
} Node;

//LRU结构
typedef struct 
{
	int capacity;
	struct Node* head;
	struct Node* tail;
	struct Node** cache;
}LRUCache;

//创建新节点
Node* createNode(int key, int value)
{
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->key = key;
	newNode->value = value;
	newNode->pre = NULL;
	newNode->next = NULL;
	return newNode;
}

//创建LRU缓存
LRUCache* creatLRUCache(int capacity)
{
	LRUCache* cache = (LRUCache*)malloc(sizeof(LRUCache));
	cache->capacity = capacity;
	cache->head = createNode(0, 0);
	cache->tail = createNode(0, 0);
	cache->head->next = cache->tail;
	cache->tail->pre = cache->head;

	cache->cache = (Node**)malloc(sizeof(Node*)*100001);
	for (int i = 0; i < 100001; i++)
	{
		cache->cache[i] = NULL;
	}
	return cache;
}

//删除节点
void deleteNode(Node* node)
{
	node->pre->next = node->next;
	node->next->pre = node->pre;
}

//插入节点到头部
void insertToHead(Node* node, Node* head)
{
	node->next = head->next;
	node->pre = head;
	head->next = node;
	node->next->pre = node;
}

int getLRUCacheValue(LRUCache* obj, int key)
{
	if (obj->cache[key] != NULL)
	{
		Node* node = obj->cache[key];
		deleteNode(node);
		insertToHead(node, obj->head);
		return node->value;
	}
	return -1;
}

void setLRUCacheValue(LRUCache* obj, int key, int value)
{
	if (obj->cache[key] != NULL)
	{
		Node* node = obj->cache[key];
		node->value = value;
		deleteNode(node);
		insertToHead(node, obj->head);
	}
	else
	{
		Node* newNode = createNode(key, value);
		obj->cache[key] = newNode;
		insertToHead(newNode, obj->head);
		if(obj->capacity-- == 0)
		{
			Node* tail = obj->tail->pre;
			deleteNode(tail);
			obj->cache[tail->key] = NULL;
			free(tail);
			obj->capacity++;
		}
	}
}

//释放内存
void LRUCacheFree(LRUCache* obj)
{
	for (int i = 0; i < 100001; i++)
	{
		if (obj->cache[i] != NULL)
		{
			free(obj->cache[i]);
		}
	}
	free(obj->cache);
	free(obj->head);
	free(obj->tail);
	free(obj);
}

int main()
{
	LRUCache* obj = creatLRUCache(2);
	setLRUCacheValue(obj, 1, 1);
	setLRUCacheValue(obj, 2, 2);
	printf("%d\n", getLRUCacheValue(obj, 1));
	setLRUCacheValue(obj, 3, 3);
	printf("%d\n", getLRUCacheValue(obj, 2));
	setLRUCacheValue(obj, 4, 4);
	printf("%d\n", getLRUCacheValue(obj, 1));
	printf("%d\n", getLRUCacheValue(obj, 3));
	printf("%d\n", getLRUCacheValue(obj, 4));
	LRUCacheFree(obj);
	system("pause");
	return 0;
}

参考:https://blog.csdn.net/Conner7/article/details/136572016

相关推荐
Boilermaker199241 分钟前
【Java EE】Mybatis-Plus
java·开发语言·java-ee
aramae1 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
Tony小周1 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
lixzest1 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
沉默媛1 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
_Chipen2 小时前
C++基础问题
开发语言·c++
止观止3 小时前
JavaScript对象创建9大核心技术解析
开发语言·javascript·ecmascript
阿捏利4 小时前
C Primer Plus 第6版 编程练习——第7章(上)
c语言·编程题·c primer plus
screenCui4 小时前
macOS运行python程序遇libiomp5.dylib库冲突错误解决方案
开发语言·python·macos
linux kernel4 小时前
第七讲:C++中的string类
开发语言·c++