1. 初始化一个双向链表。
2. 具体方法插入,每个节点可以存在如下结构:
struct node_each node{ int data; struct node_each *prev; struct node_each *next; };
A. 头插入法
代码思路:
{ D->next = head; //M2 head->prev = D; //M1 head = D; }
B 尾插入法
代码思路:
{
//body -> C if (body->next == NULL) { body->next = D; //M1 D->prev = body; //M2 } }
C 中间插入法
代码思路:
{ if (body->next == NULL) { //print nothing } else { //A as body after D body->next-prev = D; //M2 D->next = body->next; //M1 body->next = D; //M4 D->prev = body;//M3 } }
把每一条链都补全,代码迎刃而解。
Life is mess, don't let mess mess us.