关于#链表#的问题,如何解决?

c语言链表题目,输入数组和四个节点,将s1s2和t1t2之间的数交换
用例://1 2 3 4 5 6 7 8 9 10 -1
//1 1 4 7
输出:
4 5 6 7 2 3 1 8 9 10

自己检查了一下好像在for循环断掉了,运行结果没有输出,希望能帮忙解答一下,顺便改下代码能符合用例和输出


typedef struct node
{
    int number;
    struct node* nextPtr;
}Node;
int input(Node* head)
{
    int n,cnt=0;
    scanf("%d",&n);
    Node* buf=head;
    while (n!=-1)
    {
        Node* temp=(Node*)malloc(sizeof(Node));
        temp->number=n;
        temp->nextPtr=NULL;
        buf->nextPtr=temp;
        buf=buf->nextPtr;
        scanf("%d",&n);
        cnt+=1;
    }
    return cnt;
}

void output(Node* head)
{
    Node* temp=head->nextPtr;
    while (temp!=NULL)
    {
        printf("%d",temp->number);
        temp=temp->nextPtr;
    }
}
int main()
{
    Node* head=(Node*)malloc(sizeof(Node));
    int cnt=input(head);
    int s1,t1,s2,t2;
    scanf("%d %d %d %d",&s1,&s2,&t1,&t2);
    Node* s1p,*t1p,*s2p,*t2p;
    Node* s1p1=head,*t1p1=head,*s2p1=head,*t2p1=head;
    for (int i=0;i1;i++)
    {
        s1p1=s1p1->nextPtr;
        
    }
    for (int i=0;is2p=s2p->nextPtr;
        
    }
    for (int i=0;i1;i++)
    {
        t1p1=t1p1->nextPtr;
    }
    for (int i=0;it2p=t2p->nextPtr;
    }
    t1p=t1p1->nextPtr;
    t2p1=t2p->nextPtr;
    s1p=s1p1->nextPtr;
    s2p1=s2p->nextPtr; 
    
    s1p1->nextPtr=t1p;
    if (s2-t1==1)
    {
        t2p->nextPtr=s2p1;
        t1p1->nextPtr=s1p;
    }
    else
    {
        t2p->nextPtr=s1p;
    }
    s2p->nextPtr=t2p1;
    output(head);
    return 0;    
} 


//1 2 3 4 5 6 7 8 9 10 -1
//1 1 4 7

修改后运行结果:

img

修改后的代码(修改的地方有注释):


#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int number;
    struct node* nextPtr;
}Node;
int input(Node* head)
{
    int n, cnt = 0;
    scanf("%d", &n);
    Node* buf = head;
    while (n != -1)
    {
        Node* temp = (Node*)malloc(sizeof(Node));
        temp->number = n;
        temp->nextPtr = NULL;
        buf->nextPtr = temp;
        buf = buf->nextPtr;
        scanf("%d", &n);
        cnt += 1;
    }
    return cnt;
}

void output(Node* head)
{
    Node* temp = head->nextPtr;
    while (temp != NULL)
    {
        printf("%d ", temp->number); //修改1,%d后面加个空格
        temp = temp->nextPtr;
    }
}
int main()
{
    Node* head = (Node*)malloc(sizeof(Node));
    head->nextPtr = 0; //修改2,初始化头节点的nextPtr为0
    int cnt = input(head);
    int s1, t1, s2, t2;
    scanf("%d %d %d %d", &s1, &s2, &t1, &t2);
    Node* s1p, * t1p, * s2p, * t2p;
    Node* s1p1 = head, * t1p1 = head, * s2p1 = head, * t2p1 = head;
    for (int i = 0; i < s1 - 1; i++)
    {
        s1p1 = s1p1->nextPtr;
    }
    for (int i = 0; i < s2; i++)
    {
        s2p1 = s2p1->nextPtr;  //修改3
    }
    for (int i = 0; i < t1-1; i++)
    {
        t1p1 = t1p1->nextPtr; //修改
    }
    for (int i = 0; i < t2; i++)
    {
        t2p1 = t2p1->nextPtr; //修改
    }

    //修改
    s1p = s1p1->nextPtr; //暂存前半段的第一个节点
    s1p1->nextPtr = t1p1->nextPtr; //第二段的第一个节点接上链表

    t2p = t2p1->nextPtr; //暂存后半段的最后一个节点的next
    t2p1->nextPtr = s2p1->nextPtr; //把前半段最后一个节点接入链表

    t1p1->nextPtr = s1p; //前半段的第一个节点接入链表
    s2p1->nextPtr = t2p; //前半段的最后一个节点连接第二段最后一个节点的next

    output(head);
    return 0;
}


//1 2 3 4 5 6 7 8 9 10 -1
//1 1 4 7

https://blog.csdn.net/archester/article/details/115287895