问题 D 实验11_13_链表交换

问题 D 实验11_13_链表交换

题目描述
已知一个正整数序列,序列元素个数未知,但至少有两个元素,你的任务是建立一个单链表用于存储这个正整数序列。然后实现交换此链表中任意指定的两段,第一段为[s1,t1],第二段[s2,t2]。s1、t1、s2、t2代表链表的第几个节点,且满足s1<=t1,s2<=t2,t1<s2,s2一定小于等于链表节点的总个数。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。最后将链表的全部节点释放。
输入
输入一个正整数序列,以输入“-1”结束,序列中元素个数未知,但输入“-1”前至少输入两个正整数。然后是四个整数,即为s1、t1、s2、t2。
输出
经过处理后的新链表,每个元素后有一个空格,注意最后一个元素后只有换行符。
数据最多的测试用例节点数在100这个数量级,所有整数可以用int型存储。
请注意输入输出格式。
样例输入 Copy

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

样例输出 Copy

The new list is:4 5 6 7 2 3 1 8 9 10
/* creat the linked list and change it.
@author: CangCheng
@date: 14/3/2021
*/

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

struct node {
    int data;
    int *nextPtr;
};

struct node *creatLinkedList(struct node *headPtr);

void delLinkedList(struct node *headPtr);

void changeLinkedList(int s1, int t1, int s2, int t2, struct node *headPtr);

struct node *getLocation(struct node *headPtr, int num);

void printList(struct node *headPtr);

int main() {
    struct node *headPtr = NULL;
    int s1, t1, s2, t2;
    headPtr = creatLinkedList(headPtr);
    scanf("%d%d%d%d", &s1, &t1, &s2, &t2);
    changeLinkedList(s1, t1, s2, t2, headPtr);
    printList(headPtr);
    delLinkedList(headPtr);
    return 0;
}

//创建有空节点的链表
struct node *creatLinkedList(struct node *headPtr) {
    struct node *newPtr, *currentPtr;
    int num;
    newPtr = malloc(sizeof(struct node));
    newPtr->nextPtr = NULL;
    headPtr = newPtr;
    scanf("%d", &num);
    while (num != -1) {
        newPtr = malloc(sizeof(struct node));
        newPtr->data = num;
        newPtr->nextPtr = NULL;
        if (headPtr->nextPtr == NULL) {
            currentPtr = newPtr;
            headPtr->nextPtr = currentPtr;
        } else {
            currentPtr->nextPtr = newPtr;
            currentPtr = newPtr;
        }
        scanf("%d", &num);
    }
    return headPtr;
}

//删除链表
void delLinkedList(struct node *headPtr) {
    struct node *thisPtr, *thatPtr;
    thisPtr = headPtr;
    while (thisPtr != NULL) {
        thatPtr = thisPtr->nextPtr;
        free(thisPtr);
        thisPtr = thatPtr;
    }
}

//改变顺序
void changeLinkedList(int s1, int t1, int s2, int t2, struct node *headPtr) {
    struct node *s1Ptr, *t1Ptr, *s2Ptr, *t2Ptr, *preS1Ptr, *preS2Ptr, *afterT1Ptr, *afterT2Ptr;
    preS1Ptr = getLocation(headPtr, s1 - 1);
    preS2Ptr = getLocation(headPtr, s2 - 1);
    s1Ptr = preS1Ptr->nextPtr;
    t1Ptr = getLocation(headPtr, t1);
    s2Ptr = preS2Ptr->nextPtr;
    t2Ptr = getLocation(headPtr, t2);
    afterT1Ptr = t1Ptr->nextPtr;
    afterT2Ptr = t2Ptr->nextPtr;
    if (s2!=t1+1){
        t1Ptr->nextPtr = afterT2Ptr;
        preS2Ptr->nextPtr = s1Ptr;
        t2Ptr->nextPtr = afterT1Ptr;
        preS1Ptr->nextPtr = s2Ptr;
    } else{
        t1Ptr->nextPtr = afterT2Ptr;
        t2Ptr->nextPtr = s1Ptr;
        preS1Ptr->nextPtr = s2Ptr;
    }

}

//得到位置
struct node *getLocation(struct node *headPtr, int num) {
    struct node *thisPtr;
    thisPtr = headPtr;
    for (int i = 1; i <= num; i++) {
        thisPtr = thisPtr->nextPtr;
    }
    return thisPtr;
}

//打印结果
void printList(struct node *headPtr) {
    struct node *thisPtr;
    thisPtr = headPtr->nextPtr;
    printf("The new list is:");
    while (thisPtr != NULL) {
        if (thisPtr->nextPtr != NULL) {
            printf("%d ", thisPtr->data);
        } else if (thisPtr->nextPtr == NULL) {
            printf("%d\n", thisPtr->data);
        }
        thisPtr = thisPtr->nextPtr;
    }
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值