單向鏈表(無頭無循環)
1.頭插
cur->next=head;
head=cur;
2.後插
cur->next=pos->next;
pos->next=cur;
3.頭刪
tmp=head->next;
free(head);
head=tmp;
4.後刪
tmp=pos->next;
pos->next=tmp->next;
free(tmp);
5遍歷頭
for(cur=head;cur;cur=cur->next)
{
//通過cur遍歷鏈表
}
6.反轉單鏈表
<1>
oldhead->next=tmp->next;
tmp->next;=head;
head=tmp;
tmp=oldhead->next;
<2>
nt=nt->next;
cur->next==pre;
pre=cur;
cur=nt;
7.循環鏈表找入環節點
(1)找兩個指針,一個一次走一步,一個一次走兩步
(2)記錄他們的相遇節點,此時相遇節點和起始節點已經右對齊
(3)右對齊後,一起遍歷,找第一次相遇的節點,就是入環節點
雙鏈表(帶頭循環)
1.插入操作
pos->next=cur;
cur->prev=pos;
tmp->prev=cur;
cur->next=tmp;
2.刪除操作
pos->prev->next=pos->next;
pos->next->prev=pos->prev;
free(pos);
3.遍歷操作
for(cur->head->next;cur!=head;cur=cur->next)
{
//cur進行遍歷
}
4.合併操作
1.比較cur1和cur2的值,如果1比較小,那麼cur1直接向後跳轉
2.如果2的值比較小,那麼將2的這個節點前插到1的節點前面,然後2向後跳轉
3.如果循環結束後,2跳出,那麼直接結束
4.如果循環結束後,1跳出,那麼將2的剩餘所有節點插入到1的末尾