forked from Request2609/codeOfPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.cpp
More file actions
46 lines (41 loc) · 971 Bytes
/
Copy pathcache.cpp
File metadata and controls
46 lines (41 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "cache.h"
int cache::insert(shared_ptr<node>data) {
if(count == 0) {
head = data ;
head->nexts = nullptr ;
head->pre = nullptr ;
tail = head ;
}
//当前缓存还够用,将节点插入到链表尾部
if(count < num) {
tail->nexts = data ;
data->pre = tail ;
tail = data ;
count++ ;
}
if(count >=num){
head = head->nexts ;
tail->nexts = data ;
data->pre = tail ;
tail = data ;
}
return 1 ;
}
bool cache::accessData(int num) {
shared_ptr<node>cur = head ;
while(cur) {
if(cur->val == num) {
moveLast(cur) ;
return true ;
}
cur = cur->nexts;
}
return 1;
}
//将被访问的节点挪到链表尾部
void cache::moveLast(shared_ptr<node>cur) {
cur->pre->nexts = cur->nexts ;
cur->nexts->pre = cur->pre ;
tail->nexts = cur ;
cur->pre = tail ;
}