C語言之實現(xiàn)單鏈表指定結(jié)點的插入方式
單鏈表指定結(jié)點的插入
#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;//因為tail=head,所以tail沒有數(shù)值,tail->next才有數(shù)值
tail=p;
cin>>k;
}
printf("輸入要插入的數(shù):");
int m;
cin>>m;
q=new no;//生成一個結(jié)點來存放這個數(shù)
q->data=m;
q->next=NULL;
printf("要插在哪個數(shù)和哪個數(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é)點,保證后面的鏈表不斷開,
//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
要插在哪個數(shù)和哪個數(shù)之間:1 2
1 7 2 3 4 5
測試二:
一共要輸入的數(shù):5
1 1 2 3 4 0
輸入要插入的數(shù):2
要插在哪個數(shù)和哪個數(shù)之間:1 1
1 2 1 2 3 4
鏈表之在指定結(jié)點前面或后面插入新的結(jié)點
1.我們知道鏈表無非就是增刪改查這幾個操作,而在指定結(jié)點前方或后方插入結(jié)點就是增加的一種體現(xiàn)。
(1)在指定結(jié)點后方插入新結(jié)點
例子:1->2->3->4->5
當(dāng)要在3和4之間插入新結(jié)點的時候(即在3的后方插入新結(jié)點)
步驟:
- 1.先找到3(point->data == data)
- 2.將3->next(即4的地址)賦給new->next,將新結(jié)點new與結(jié)點4建立了聯(lián)系
即:new->next = 3->next;
- 3.完成上面兩步驟后,再執(zhí)行3->next = new;這樣就完成了在3的后面插入了新的結(jié)點
切記步驟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é)點前方插入新結(jié)點
例子:1->2->3->4->5
在指定結(jié)點前方插入新結(jié)點要考慮兩種情況,一種是特殊情況,看是否插入的位置是在鏈表頭,另一種情況是正常插入,插入的地方不是鏈表頭,那么我們一起來看看你兩種情況怎么辦。
1.如果要插入的點剛好是在鏈表頭的話
?? ?struct Test* point = head;
?? ?if(point->data == data)
?? ?{
?? ??? ?new->next = head;
?? ??? ?return new;
?? ?}//如果要找的那個數(shù)剛好在頭結(jié)點的話那么直接插進去即可以上兩個操作就完成了在鏈表頭插入新的結(jié)點的操作,但是記得return的是new,而不是head,因為現(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),一定要切記這兩點
直接上代碼:
struct Test* insertBehindLinklist(struct Test *head,struct Test* new,int data)
{//前插法的做法
?? ?struct Test* point = head;
?? ?if(point->data == data)
?? ?{
?? ??? ?new->next = head;
?? ??? ?return new;
?? ?}//如果要找的那個數(shù)剛好在頭結(jié)點的話那么直接插進去即可。
?? ?
?? ?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;
}切記一點:
如果鏈表頭發(fā)生了改變的話,一定要返回新的鏈表頭,如果鏈表頭沒發(fā)生變化的話,不用返回鏈表頭,在main里也能檢測到鏈表結(jié)點的變化(因為指針本身就是一個地址)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

