雙向鏈表的三種插入方法研究


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