雙向鍊錶的建立 結點的插入 刪除與列印

2021-06-22 05:16:59 字數 1872 閱讀 7547

(1)雙向鍊錶的建立與單向鍊錶是類似的。其核心也是結點的記憶體申請以及結點間前後趨關係。

建立如下雙鏈表:

//建立雙向鍊錶

pnode createdoublelist(type *val,int n)

head->pre=null;

head->next=null;

temp=head;

for(int i=0;idata=val[i];

temp->next=newnode;

newnode->pre=temp;

newnode->next=null;

temp=newnode;

} return head;

}//列印雙向鍊錶

void printdoublelist(pnode head)

printf("\n");

}執行結果為

(2)向雙向鍊錶中插入乙個結點。分為所插入結點號是否是為尾結點,應分類寫出相關**。

//雙鏈表中插入乙個結點

pnode insertnode(pnode head,int num,type data)

p->data=data;

if(num==m)

p->next=null;

temp->next=p;

p->pre=temp;

return head;

}else if((num>0)&&(numnext;

num--;

} p->next=temp->next;

temp->next->pre=p;

temp->next=p;

p->pre=temp;

return head;

}else

}

執行結果為:

(3)刪除乙個結點  找到刪除結點的前一結點,並對前後關係進行處理、

**如下:

//雙鏈表中輸出乙個結點 

pnode deletenode(pnode head, int num)

temp->next=temp->next->next;

temp->next->next->pre=temp;

return head;

} else }

int _tmain(int argc, _tchar* argv)

;pnode head=createdoublelist(a,6);

printdoublelist(head);

pnode p=insertnode(head,5,31);

printdoublelist(p);

pnode tp=deletenode(p,5);

if(null!=tp)

else

printf("刪除結點失敗!");

return 0;

}

執行結果為

雙向鍊錶的建立插入與刪除

雙向鍊錶的建立插入與刪除 注意因為建表時用了字元型資料所以輸入數字時只能輸0 9 include includetypedef struct node node,list list tailcreat 尾插法建立鍊錶 void insert list head,int x,char value,in...

雙向迴圈鍊錶的插入與刪除

關於解釋部分不再多說了,網上資料很多,下面就介紹具體的實現吧 雙向迴圈鍊錶的插入與刪除 typedef struct nodednode,dlinklist 在帶有頭結點雙向迴圈鍊錶中第1個資料域內容為x的結點右邊插入乙個資料資訊為item的新結點 void insert dlinklist lis...

雙向迴圈鍊錶(建立 插入 刪除 遍歷)

author chen ming dong include include typedef struct list str int n str creat str head head prior p p next head return head 遍歷 void gothrough str head...