思维导图:

双向循环链表头插入:
代码:
Doublelist insert_head(Doublelist head,datatype element)
{
//创建新节点s
Doublelist s=create_node();
if(NULL==s)
{
return head;
}
s->data=element;//数据存储
//判断链表是否为空
if(NULL==head)
{
head=s;
return head;
}
else
{
Doublelist rear=head->priv;
//开始插入
s->next=head;//把原来的头放到s的后面
head->priv=s;//原来的head前指针指向s
head=s;//s成为新的头
//形成两个环
head->priv=rear;
rear->next=head;
return head;
}
}
画图:

运行效果:

双向循环链表尾插入:
代码:
//尾插
Doublelist insert_rear(Doublelist head,datatype element)
{
//创建新节点s
Doublelist s=create_node();
if(NULL==s)
{
return head;
}
s->data=element;//数据存储
//判断链表是否为空
if(NULL==head)
{
head=s;
return head;
}
else
{
Doublelist p=head;
//找最后一个节点
while(p->next!=head)
{
p=p->next;
}
//开始插入
p->next=s;//把数据存放s放到的最后一位的后面
s->priv=p;//s的前一个是原来的最后一个成为新的最后一个s
s->next=head;
head->priv=s;
return head;
}
}
画图:

运行效果:

双向循环链表头删除
代码:
//头删除
Doublelist del_head(Doublelist head)
{
if(NULL==head)
{
return head;
}
if(head->next==head)
{
free(head);
head=NULL;
return head;
}
else
{
Doublelist rear=head->priv;//定位到最后一个节点,并存储
Doublelist del=head;//要删除的头标志到del
head=head->next;//头节点变成原来的下一个
free(del);//释放del
del=NULL;//防止del
//形成环
rear->next=head;
head->priv=rear;
return head;
}
}
画图:

运行效果:

双向循环链表尾删除
代码:
//尾巴删除
Doublelist del_rear(Doublelist head)
{
if(NULL==head)
{
return head;
}
else if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
else
{
Doublelist p=head;
//找倒数第二个节点
while(p->next->next!=head)
{
p=p->next;
}
//标记倒数第一个
Doublelist del=p->next;
free(del);//释放最后一个
del=NULL;//防止野指针
p->next=head;//新的最后一个指向空
head->priv=p;
return head;
}
}
画图:

运行效果:
