双向链表的三种插入方法研究


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
  }
}

 

 把每一条链都补全,代码迎刃而解。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM