LRU Cache实现

设计并实现一个最近最少使用(LRU)缓存结构。它应该支持get和put操作。当缓存容量达到上限时,会淘汰最久未使用的项。实现中使用了单链表结合STL库的map来提升查找效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.


采用单链表,并通过STL库的map来提高搜索速度。

</pre><pre code_snippet_id="116512" snippet_file_name="blog_20131217_2_7850229" name="code" class="cpp">class LRUCache
{
public:
  LRUCache(int capacity)
  {
    capacity_ = capacity;
	length_ = 0;
	list_ = NULL;
  }
  ~LRUCache(){ Destroy();}
  int get(int key);
  void set(int key, int value);
  void Print();
private:
  struct ListNode
  {
    int key;
    int value;
    struct ListNode *next;
	struct ListNode *pre;
    ListNode(int k=-1,int val=-1, struct ListNode *p=NULL)
      : key(k), value(val), next(p), pre(p){}
  };
  typedef struct ListNode ListNode;

  void Destroy();
  void Delete(ListNode *node);
  void DeleteRear();
  ListNode* Find(int key);
  ListNode* InsertFront(int key, int value);

  int length_;
  int capacity_;
  ListNode *list_;
  map<int, ListNode*> map_;
};

//value is positive
//if not found, return -1
int LRUCache::get(int key)
{
  int x;
  ListNode *node(NULL);
  if(map_.count(key))
  {
	node = map_.find(key)->second;
	Delete(node);//do not free space
	node->next = list_;
	list_ = node;
    return node->value;
  }
  else return -1;
}

//if full, delete the least used, and insert into the head
void LRUCache::set(int key, int value)
{
  ListNode *node(NULL);
  if(map_.count(key))
  { //already in the list
    node = map_.find(key)->second;
    Delete(node);
	node->value = value;
	node->next = list_;
	list_ = node;
  }
  else
  { //not found
    node = InsertFront(key, value);
	map_[key] = node;
	if(length_ >= capacity_)
	  DeleteRear();
	else
	  length_++;
  }
}

//return pointer to the node containing key
//if not found, return NULL
typename LRUCache::ListNode* LRUCache::Find(int key)
{
  ListNode *list(list_);

  while(list && list->key != key)
    list = list->next;
  return (list!=NULL) ? list:NULL;
}

//destroy singlely-linked list
void LRUCache::Destroy()
{
  ListNode *pre(NULL);

  while(list_)
  {
    pre = list_;
	list_ = list_->next;
	delete pre;
  }
  map_.clear();
}

void LRUCache::Delete(ListNode *node)
{ // do not free space
  assert(node != NULL);
  ListNode *pre(NULL), *cur(list_);

  while(cur != node)
  {
    pre = cur;
	cur = cur->next;
  }
  assert(cur == node);
  if(pre == NULL)
    list_ = node->next;
  else
    pre->next = cur->next;
}

void LRUCache::DeleteRear()
{
  if(list_ == NULL) return;
  ListNode *cur(list_), *pre(NULL);

  while(cur->next)
  {
    pre = cur;
	cur = cur->next;
  }
  if(pre == NULL)
    list_ = cur->next;
  else
    pre->next = cur->next;
  map<int, ListNode*>::iterator it = map_.find(cur->key);
  map_.erase(it);
  delete cur;
}

LRUCache::ListNode* LRUCache::InsertFront(int key, int val)
{
  ListNode *node = new ListNode(key, val, list_);
  list_ = node;
  return node;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值