Skip to content

Commit 2984168

Browse files
Merge pull request wangzheng0822#268 from Devil0F/patch-3
添加注释
2 parents d97d22a + e3580e3 commit 2984168

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

c-cpp/06_linkedlist/Dlist/Dlist.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void dlist_init(stDlistHead *dlist) //链表初始化
1919
return;
2020
}
2121

22-
void dlist_destory(stDlistHead *dlist)
22+
void dlist_destory(stDlistHead *dlist)//删除链表
2323
{
2424
stDlistNode *pNode = NULL;
2525

@@ -40,7 +40,7 @@ int dlist_insert_head(stDlistHead *dlist,stDlistNode *pNode,int data) //插入
4040
{
4141
if(pNode == NULL)//当只传递一个数据时
4242
{
43-
pNode = (stDlistNode *)malloc(sizeof(stDlistNode));//新建节点,为节点分配空间
43+
pNode = (stDlistNode *)malloc(sizeof(stDlistNode));//新建节点,为节点分配空间(malloc()可能需要#include<malloc.h>)
4444
if (pNode == NULL)
4545
{
4646
return -1;
@@ -67,7 +67,7 @@ int dlist_insert_head(stDlistHead *dlist,stDlistNode *pNode,int data) //插入
6767
return 0;
6868
}
6969

70-
stDlistNode * dlist_remove_tail(stDlistHead *dlist)//删除尾部节点
70+
stDlistNode * dlist_remove_tail(stDlistHead *dlist)//删除尾部节点,并返回删除节点
7171
{
7272
stDlistNode *pNode = NULL;
7373

@@ -91,7 +91,7 @@ stDlistNode * dlist_remove_tail(stDlistHead *dlist) //删除尾部节点
9191
return pNode;
9292
}
9393

94-
void dlist_remove_node(stDlistHead * dlist,stDlistNode *pNode)
94+
void dlist_remove_node(stDlistHead * dlist,stDlistNode *pNode) //删除指定节点
9595
{
9696
if ((dlist == NULL)||(pNode == NULL))
9797
{
@@ -119,12 +119,12 @@ void dlist_remove_node(stDlistHead * dlist,stDlistNode *pNode)
119119

120120
if (dlist->size == 0)
121121
{
122-
memset(dlist,0,sizeof(stDlistHead));
122+
memset(dlist,0,sizeof(stDlistHead)); //将dlist占用内存块的所有值置为0,也就是清空head,tail指针内容
123123
}
124124

125125
return;
126126
}
127-
stDlistNode * dlist_search(stDlistHead * dlist,int data)
127+
stDlistNode * dlist_search(stDlistHead * dlist,int data) //根据值搜索节点,并返回
128128
{
129129
stDlistNode *pNode = dlist->head;
130130
while(pNode != NULL)
@@ -153,16 +153,16 @@ void dlist_dump(stDlistHead *dlist) //显示链表中的数据
153153
}
154154

155155

156-
void Lru_dlist(stDlistHead *dlist,int data)
156+
void Lru_dlist(stDlistHead *dlist,int data) //LRU(最近最少使用)缓存淘汰算法
157157
{
158158
stDlistNode *pNode = NULL;
159159

160-
pNode = dlist_search(dlist,data);
161-
if (pNode != NULL)
160+
pNode = dlist_search(dlist,data);
161+
if (pNode != NULL) //如果在链表中找到这个值,则删除储存这个值的节点,之后吧这个节点放在头部
162162
{
163163
dlist_remove_node(dlist,pNode);
164164
}
165-
else if(dlist->size >= 4)
165+
else if(dlist->size >= 4)//没在链表中找到,且链表长度大于4,则从链表中删除尾部节点,将新数据放在头部
166166
{
167167
pNode = dlist_remove_tail(dlist);
168168

0 commit comments

Comments
 (0)