本部分实现双向循环链表的:
- 统计链表中结点个数
- 删除某个结点
(根据结构体中某个成员的值、根据处于链表中的位置)
(有头节点、无头结点的处理区别) - 链表的销毁及野指针处理
双向循环链表_第2部分
1 统计链表中的结点个数
1.1 带头结点
int CountingNodesOfListWithHead(node *list){
if(list == NULL){
// Defensive programming
return 0;
}
int cnt = 0;
for(node *p = list->pNext; p != list; p = p->pNext){
cnt++;
}
return cnt;
}
1.2 不带头结点
int CountingNodesOfList(node *list){
if(list == NULL){
// Defensive programming
return 0;
}
int cnt = 0;
node *p = list;
do{
cnt++;
p = p->pNext;
}while(p != list);
return cnt;
}
2 查找链表中的数据
2.1 查找带头结点的链表
node *SearchListWithHead(node *list, int value){
for(node *p = list->pNext; p != list; p = p->pNext){
if(p->num == value){
// founded
return p;
}
}
return NULL; // not founded
}
2.2 查找不带头节点的链表
node *SearchList(node *list, int value){
node *p = list;
do{
if(p->num == value){
return p;
}
p = p->pNext;
}while(p != list);
return NULL;
}
3 根据位置删除某个节点
3.1 带头结点
3.1.1 正数第k个位置
int RemoveNodeFromListWithHead_ByHeadPos(node *list, int pos_from_head){
// The range of "pos_from_head" is [1 .. n]
if(pos_from_head < 1){
// Defensive programming
return