以前一致一直用单向链表,而且对链表的理解模模糊糊,经过一段时间的理解刷题,略微懂了链表的操作,下面贴一下我不借助任何书自己通过草稿想出来的双向循环聊表链表基本操作
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int num;
struct Test *prior;
struct Test *next;
};
/*头插*/
void head_insert(struct Test *head, struct Test *newnode)
{
newnode->next = head->next;
newnode->prior = head;
head->next->prior = newnode;
head->next = newnode;
}
/*尾插,无需像单向循环链表位移到底,head->prior既是尾,减少时间复杂度*/
void tail_insert(struct Test *head, struct Test *newnode)
{
newnode->next = head;
newnode->prior = head->prior;
head->prior->next = newnode;
head->prior = newnode;
}
/*中间插,可以头尾同时寻找,first找到就插在当前数据下一个,end先找到就插在当前数据前一个,减少时间复杂度*/
void mid_insert(struct Test *head, struct Test *newnode, int num)
{
if(head->next == head)
{
return;
}
struct Test *first_find = head->next;
struct Test *end_find = head->prior;
while(first_find != head && end_find != head)
{
if(first_find->num == num)
{
newnode->next = first_find->next;
newnode->prior = first_find;
first_find->next->prior = newnode;
first_find->next= newnode;
return;
}
if(end_find->num == num)
{
newnode->prior = end_find->prior;
newnode->next= end_find;
end_find->prior->next = newnode;
end_find->prior = newnode;
return;
}
first_find = first_find->next;
end_find = end_find->prior;
}
}
/*删除,思想和中间插一样*/
void delete(struct Test *head, int num)
{
if(head->next == head)
{
return;
}
struct Test *first_find = head->next;
struct Test *end_find = head->prior;
while(first_find != head && end_find != head)
{
if(first_find->num == num)
{
first_find->prior->next = first_find->next;
first_find->next->prior = first_find->prior;
return;
}
if(end_find->num == num)
{
end_find->prior->next = end_find->next;
end_find->next->prior = end_find->prior;
return;
}
first_find = first_find->next;
end_find = end_find->prior;
}
}
/*顺序遍历显示*/
void first_display(struct Test *head)
{
if(head->next == head)
{
printf("no elements!\n");
return;
}
struct Test *temp = head->next;
while(temp != head)
{
printf("%d", temp->num);
temp = temp->next;
}
}
/*逆序遍历显示*/
void end_display(struct Test *head)
{
if(head->prior == head)
{
printf("no elements!\n");
return;
}
struct Test *temp = head->prior;
while(temp != head)
{
printf("%d", temp->num);
temp = temp->prior;
}
}
int main()
{
struct Test *head = (struct Test *)malloc(sizeof(struct Test));
head->next = head;
head->prior = head;
int num;
int choose;
while(1)
{
printf("1.head 2.tail 3.mid 4.first_d 5.end_di 6.delete\n");
scanf("%d", &choose);
if(choose == 1)
{
scanf("%d", &num);
struct Test *newnode = (struct Test *)malloc(sizeof(struct Test));
newnode->num = num;
head_insert(head, newnode);
}
else if(choose == 2)
{
scanf("%d", &num);
struct Test *newnode = (struct Test *)malloc(sizeof(struct Test));
newnode->num = num;
tail_insert(head, newnode);
}
else if(choose == 3)
{
scanf("%d", &num);
int m;
scanf("%d", &m);
struct Test *newnode = (struct Test*)malloc(sizeof(struct Test));
newnode->num = num;
mid_insert(head, newnode, m);
}
else if(choose == 4)
{
first_display(head);
}
else if(choose == 5)
{
end_display(head);
}
else if(choose == 6)
{
scanf("%d", &num);
delete(head, num);
}
}
}
综上所述,双向循环链表对比与单项循环链表时间复杂度降低了,就相当与你出去旅游,按顺序一个个去不同地方的景点,中间有一站上海,上海后一站张家港,你现在在张家港,如果是单向循环链表,你必须按顺序浏览到尾在从头到上海,相当于麦哲伦绕地球一圈在回到起点。而双向循环链表意味着你可以返航,从张家港返回上海,这就大大节省了你的时间。
还有头插,相当于往手枪内填子弹,最先被插入的子弹被新的子弹不断往后挤,被先射出枪膛的权利(head指针指向的位置)不断赋予给新的子弹,这是不带头节点,而如果是带头节点的链表,相当于手枪内的第一颗子弹永远是一个空心弹(头节点不放数据),而被先射出枪膛的权利永远是它的(head指针指向头节点空间最好不要变)。而它的头插就是你明知道第一个空心弹,还必须插在空心弹之后,而先插的子弹必须被新的子弹一步步往后排挤。
而对于尾插,单向循环的链表就是位移到最后一位再插数据,而双向的循环链表head->prior就是链表的最后,相当简单,更别说双向能从头尾同时查找,所以双向循环链表时间复杂度要远小于单向循环链表,虽然编程要多写一点。
链表操作最重要的一点是先操作要插入的节点,再操作链表的原节点,前后顺序颠倒会有大问题,类似死循环之类的错误
例子: temp->next = p;
p->next = temp->next;
temp 的next指针已经存放p的地址
再将p的next指针存放temp->next存放的地址,即p。所以p->next = p,死循环必须的。