您的位置:首页 > 理论基础 > 数据结构算法

数据结构-带头结点带环的双向链表基本操作

2018-04-03 16:47 615 查看
首先,带头结点带环的双向链表的特点:

带头节点:但是我们并不关心头节点中的元素,只起“带头”的作用。

带环

双向:要求每个结点中都有一个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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: