日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言之實現單鏈表指定結點的插入方式_C 語言

作者:nefu_lmy ? 更新時間: 2022-09-05 編程語言

單鏈表指定結點的插入

#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("一共要輸入的數:");
    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沒有數值,tail->next才有數值
        tail=p;
        cin>>k;
    }
    printf("輸入要插入的數:");
    int m;
    cin>>m;
    q=new no;//生成一個結點來存放這個數
    q->data=m;
    q->next=NULL;
    printf("要插在哪個數和哪個數之間:");
    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;//先處理后面的結點,保證后面的鏈表不斷開,
           //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;
}

測試一:

一共要輸入的數:5
1 2 3 4 5 0
輸入要插入的數:7
要插在哪個數和哪個數之間:1 2
1 7 2 3 4 5?

測試二:

一共要輸入的數:5
1 1 2 3 4 0
輸入要插入的數:2
要插在哪個數和哪個數之間:1 1
1 2 1 2 3 4

鏈表之在指定結點前面或后面插入新的結點

1.我們知道鏈表無非就是增刪改查這幾個操作,而在指定結點前方或后方插入結點就是增加的一種體現。

(1)在指定結點后方插入新結點

例子:1->2->3->4->5

當要在3和4之間插入新結點的時候(即在3的后方插入新結點)

步驟:

  • 1.先找到3(point->data == data)
  • 2.將3->next(即4的地址)賦給new->next,將新結點new與結點4建立了聯系

即:new->next = 3->next;

  • 3.完成上面兩步驟后,再執行3->next = new;這樣就完成了在3的后面插入了新的結點

切記步驟2和3不可調換。

直接上代碼:

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)在指定結點前方插入新結點

例子:1->2->3->4->5

在指定結點前方插入新結點要考慮兩種情況,一種是特殊情況,看是否插入的位置是在鏈表頭,另一種情況是正常插入,插入的地方不是鏈表頭,那么我們一起來看看你兩種情況怎么辦。

1.如果要插入的點剛好是在鏈表頭的話

?? ?struct Test* point = head;
?? ?if(point->data == data)
?? ?{
?? ??? ?new->next = head;
?? ??? ?return new;
?? ?}//如果要找的那個數剛好在頭結點的話那么直接插進去即可

以上兩個操作就完成了在鏈表頭插入新的結點的操作,但是記得return的是new,而不是head,因為現在的head已經發生了改變。

2.如果不是在鏈表頭,即正常插入

while(point->next != NULL)//這里跟后插法的區別,后插法是直接判斷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;
?? ?}//如果要找的那個數剛好在頭結點的話那么直接插進去即可。
?? ?
?? ?while(point->next != NULL)//這里跟后插法的區別,后插法是直接判斷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;
}

切記一點:

如果鏈表頭發生了改變的話,一定要返回新的鏈表頭,如果鏈表頭沒發生變化的話,不用返回鏈表頭,在main里也能檢測到鏈表結點的變化(因為指針本身就是一個地址)

原文鏈接:https://blog.csdn.net/nefu_lmy/article/details/119108089

欄目分類
最近更新