数据结构-带头结点带环的双向链表基本操作
2018-04-03 16:47
615 查看
首先,带头结点带环的双向链表的特点:
带头节点:但是我们并不关心头节点中的元素,只起“带头”的作用。
带环
双向:要求每个结点中都有一个next指针指向下一个,一个prev指针指向上一个
带头节点:但是我们并不关心头节点中的元素,只起“带头”的作用。
带环
双向:要求每个结点中都有一个next指针指向下一个,一个prev指针指向上一个
初始化
void DLinkListInit(DLinkNode** phead) { if(phead==NULL) { return;//非法输入 } *phead=CreatDNode(0); }
尾插尾删
void DLinkListPushBack(DLinkNode* head, DLinkType value) { if(head==NULL) { return; } DLinkNode* new_node=CreatDNode(value); DLinkNode* to_insert_prev=head->prev; new_node->next=head; head->prev=new_node; to_insert_prev->next=new_node; new_node->prev=to_insert_prev; }
头插头删
void DLinkListPushFront(DLinkNode* head, DLinkType value) { if(head==NULL) { return; } DLinkNode* to_insert=head->next; DLinkNode* new_node=CreatDNode(value); new_node->next=to_insert; to_insert->prev=new_node; head->next=new_node; new_node->prev=head; } void DLinkListPopFront(DLinkNode* head) { if(head==NULL) { return; } if(head->next==head) { return; } DLinkNode* to_delete=head->next; DLinkNode* to_delete_next=to_delete->next; head->next=to_delete_next; to_delete_next->prev=head; DestroyDNode(to_delete); }
查找指定位置的元素
DLinkNode* DLinkListFind(DLinkNode* head, DLinkType to_find) { if(head==NULL) { return NULL; } DLinkNode* cur=head->next; for(;cur!=head;cur=cur->next) { if(cur->data==to_find) { return cur; } } return NULL; }
在指定位置之前插入
void DLinkListInsert(DLinkNode* head,DLinkNode* pos, DLinkType value) { if(head==NULL||pos==NULL) { return; } DLinkNode* cur=pos->prev; DLinkNode* new_node=CreatDNode(value); new_node->next=pos; pos->prev=new_node; cur->next=new_node; new_node->prev=cur; }
在指定位置之后插入
void DLinkListInsertAfter(DLinkNode* head,DLinkNode* pos, DLinkType value) { if(head==NULL||pos==NULL) { return; } DLinkNode* to_insert_next=pos->next; DLinkNode* new_node=CreatDNode(value); new_node->next=to_insert_next; to_insert_next->prev=new_node; pos->next=new_node; new_node->prev=pos; }
删除指定位置的元素
void DLinkListErase(DLinkNode* head,DLinkNode* pos) { if(head==NULL||pos==NULL) { return; } DL 4000 inkNode* to_delete_next=pos->next; DLinkNode* to_delete_prev=pos->prev; to_delete_prev->next=to_delete_next; to_delete_next->prev=to_delete_prev; DestroyDNode(pos); }
删除指定值
void DLinkListRemove(DLinkNode* head,DLinkType value) { if(head==NULL) { return; } DLinkNode* cur=head->next; for(;cur!=head;cur=cur->next) { if(cur->data==value) { DLinkNode* tmp=cur; cur->prev->next=cur->next; cur->next->prev=cur->prev; DestroyDNode(tmp); return; } } }
删除所有指定值
} void DLinkListRemoveAll(DLinkNode* head,DLinkType value) { if(head==NULL) { return; } DLinkNode* cur=head->next; for(;cur!=head;cur=cur->next) { if(cur->data==value) { DLinkNode* tmp=cur; cur->prev->next=cur->next; cur->next->prev=cur->prev; DestroyDNode(tmp); } } }
判断链表的大小
size_t DLinkListSize(DLinkNode* head) { if(head==NULL) { return (size_t)-1; } if(head->next==head) { return 0; } DLinkNode* cur=head->next; size_t count=0; while(cur!=head) { count++; cur=cur->next; } return count; }
判断链表是否为空
int DLinkListEmpty(DLinkNode* head) { if(head==NULL) { return -1; } if(head->next==head) { return 0; } return 1; }
相关文章推荐
- 《数据结构》进行曲--带头结点的单循环链表的基本操作
- 带头节点、带环的双向链表的一些基本操作
- !-- 带头结点带环的双向链表的相关操作实现 --!
- C语言实现双向非循环链表(带头结点尾结点)的基本操作
- 带头结点的循环链表基本操作
- bo2-8.cpp 不带头结点的单链表(存储结构由c2-2.h定义)的部分基本操作(9个)
- C语言实现单链表(带头结点)的基本操作
- 带头结点的单链表的12个基本操作(C++)
- 数据结构学习之双向循环链表的基本操作(非递归实现)
- 带头结点的单链表的12个基本操作
- bo2-9.cpp 不带头结点的单链表(存储结构由c2-2.h定义)的部分基本操作(2个)
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- 带头结点的单链表的12个基本操作
- 【数据结构】双向链表的基本操作
- !-- 不带头结点不带环的单链表相关操作实现 --!
- 带头带环双向链表的实现及其相关操作。。
- 数据结构之双向带头节点带环链表的实现
- 带头结点的单链表的八种基本操作
- 带头结点的链表基本操作
- 数据结构 - C语言版 - 双向链表 所有基本操作