C語言之實(shí)現(xiàn)單鏈表指定結(jié)點(diǎn)的插入方式
單鏈表指定結(jié)點(diǎn)的插入
#include <bits/stdc++.h> using namespace std; typedef struct node { int data; struct node *next; }no; int main() { no *head,*tail,*p,*q; head=new no; head->next=NULL; tail=head; int n; printf("一共要輸入的數(shù):"); cin>>n; int k; cin>>k; for(int i=0;i<n;i++) { p=new no; p->data=k; p->next=NULL; tail->next=p;//因?yàn)閠ail=head,所以tail沒有數(shù)值,tail->next才有數(shù)值 tail=p; cin>>k; } printf("輸入要插入的數(shù):"); int m; cin>>m; q=new no;//生成一個(gè)結(jié)點(diǎn)來存放這個(gè)數(shù) q->data=m; q->next=NULL; printf("要插在哪個(gè)數(shù)和哪個(gè)數(shù)之間:"); int a,b; cin>>a>>b; p=head; while(p->data!=m&&p->next!=NULL) { p=p->next; if(p->data==a&&p->next->data==b) { q->next=p->next;//先處理后面的結(jié)點(diǎn),保證后面的鏈表不斷開, //q->next可以起到鏈接后面鏈表的作用 p->next=q; } } p=head->next; for(int j=0;j<n+1;j++) { printf("%d ",p->data); p=p->next; } return 0; }
測試一:
一共要輸入的數(shù):5
1 2 3 4 5 0
輸入要插入的數(shù):7
要插在哪個(gè)數(shù)和哪個(gè)數(shù)之間:1 2
1 7 2 3 4 5
測試二:
一共要輸入的數(shù):5
1 1 2 3 4 0
輸入要插入的數(shù):2
要插在哪個(gè)數(shù)和哪個(gè)數(shù)之間:1 1
1 2 1 2 3 4
鏈表之在指定結(jié)點(diǎn)前面或后面插入新的結(jié)點(diǎn)
1.我們知道鏈表無非就是增刪改查這幾個(gè)操作,而在指定結(jié)點(diǎn)前方或后方插入結(jié)點(diǎn)就是增加的一種體現(xiàn)。
(1)在指定結(jié)點(diǎn)后方插入新結(jié)點(diǎn)
例子:1->2->3->4->5
當(dāng)要在3和4之間插入新結(jié)點(diǎn)的時(shí)候(即在3的后方插入新結(jié)點(diǎn))
步驟:
- 1.先找到3(point->data == data)
- 2.將3->next(即4的地址)賦給new->next,將新結(jié)點(diǎn)new與結(jié)點(diǎn)4建立了聯(lián)系
即:new->next = 3->next;
- 3.完成上面兩步驟后,再執(zhí)行3->next = new;這樣就完成了在3的后面插入了新的結(jié)點(diǎn)
切記步驟2和3不可調(diào)換。
直接上代碼:
int inserFrontLinklist(struct Test* head,struct Test* new,int data1) { ?? ?struct Test* point = head; ?? ?while(point != NULL) ?? ?{ ?? ??? ?if(point->data == data1) ?? ??? ?{ ?? ??? ??? ?//后插法的做法 ?? ??? ??? ?new->next = point->next; ?? ??? ??? ?point->next = new; ?? ??? ??? ?return 1; ?? ??? ?} ?? ??? ?point = point->next; ?? ?}?? ? ?? ?return 0; }
(2)在指定結(jié)點(diǎn)前方插入新結(jié)點(diǎn)
例子:1->2->3->4->5
在指定結(jié)點(diǎn)前方插入新結(jié)點(diǎn)要考慮兩種情況,一種是特殊情況,看是否插入的位置是在鏈表頭,另一種情況是正常插入,插入的地方不是鏈表頭,那么我們一起來看看你兩種情況怎么辦。
1.如果要插入的點(diǎn)剛好是在鏈表頭的話
?? ?struct Test* point = head; ?? ?if(point->data == data) ?? ?{ ?? ??? ?new->next = head; ?? ??? ?return new; ?? ?}//如果要找的那個(gè)數(shù)剛好在頭結(jié)點(diǎn)的話那么直接插進(jìn)去即可
以上兩個(gè)操作就完成了在鏈表頭插入新的結(jié)點(diǎn)的操作,但是記得return的是new,而不是head,因?yàn)楝F(xiàn)在的head已經(jīng)發(fā)生了改變。
2.如果不是在鏈表頭,即正常插入
while(point->next != NULL)//這里跟后插法的區(qū)別,后插法是直接判斷point本身,而這里是判斷point->next ?? ?{ ?? ??? ?if(point->next->data == data)//這里也一樣,后插法是判斷point->data,而這里是判斷point->next->data; ?? ??? ?{ ?? ??? ??? ?new->next = point->next;//這里的算法跟后插法一樣 ?? ??? ??? ?point->next = new; ?? ??? ??? ? ?? ??? ??? ?printf("Congratulations,you find the data success!\n"); ?? ??? ??? ?return head; ?? ??? ?} ?? ??? ?point = point->next; ?? ?}
雖然這里的核心代碼跟上面的后插法是一樣的,但是兩者的判斷條件不一樣,后插法的判斷條件是while(point != NULL),而前插法的判斷條件是while(point->next != NULL),一定要切記這兩點(diǎn)
直接上代碼:
struct Test* insertBehindLinklist(struct Test *head,struct Test* new,int data) {//前插法的做法 ?? ?struct Test* point = head; ?? ?if(point->data == data) ?? ?{ ?? ??? ?new->next = head; ?? ??? ?return new; ?? ?}//如果要找的那個(gè)數(shù)剛好在頭結(jié)點(diǎn)的話那么直接插進(jìn)去即可。 ?? ? ?? ?while(point->next != NULL)//這里跟后插法的區(qū)別,后插法是直接判斷point本身,而這里是判斷point->next ?? ?{ ?? ??? ?if(point->next->data == data)//這里也一樣,后插法是判斷point->data,而這里是判斷point->next->data; ?? ??? ?{ ?? ??? ??? ?new->next = point->next;//這里的算法跟后插法一樣 ?? ??? ??? ?point->next = new; ?? ??? ??? ? ?? ??? ??? ?printf("Congratulations,you find the data success!\n"); ?? ??? ??? ?return head; ?? ??? ?} ?? ??? ?point = point->next; ?? ?} ?? ?printf("Sorry, you find error!\n"); ?? ? ?? ?return head; }
切記一點(diǎn):
如果鏈表頭發(fā)生了改變的話,一定要返回新的鏈表頭,如果鏈表頭沒發(fā)生變化的話,不用返回鏈表頭,在main里也能檢測到鏈表結(jié)點(diǎn)的變化(因?yàn)橹羔槺旧砭褪且粋€(gè)地址)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++生成dll和調(diào)用dll的方法實(shí)例
C++生成dll和調(diào)用dll的方法實(shí)例,需要的朋友可以參考一下2013-03-03C語言動(dòng)態(tài)規(guī)劃之背包問題詳解
這篇文章主要介紹了C語言動(dòng)態(tài)規(guī)劃之背包問題詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04