问题 E 实验11_9_链表归并

问题 E 实验11_9_链表归并

题目描述
已知有两个递增的正整数序列A和B,序列中元素个数未知,同一序列中不会有重复元素出现,有可能某个序列为空。现要求将序列B归并到序列A中,且归并后序列A的数据仍然按递增顺序排列。如果序列B中某些数据在序列A中也存在,则这些数据所在节点仍然留在序列B中,而不被归并到序列A中;否则这些数据所在节点将从序列B中删除,添加到序列A中。
要求:
建立两个单链表A、B用于存储两个正整数序列,然后按照题目的要求,将链表B中的元素归并到链表A中。在归并的过程中,不要释放B中的节点空间、然后建立新节点,而要改变指针的指向,使元素从B中删除并添加到A中。正整数序列按照递增顺序输入,用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。在程序结束前要释放链表A、B中的所有节点。
输入
依次输入两个递增的正整数序列A和B,序列元素的个数未知,但以输入“-1”结束,每个正整数序列占一行。
输出
处理后的链表A中的元素,占一行;然后是处理后的链表B中的元素,占一行。每行的每个元素后有一个空格,注意最后一个元素后只有换行符,如果某个链表为空则,则输出“There is no item in X list.”
数据最多的测试用例节点数在100这个数量级,所有整数可以用int型存储。
请注意输入输出格式。
样例输入 Copy

Sample 1:

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


Sample 2:
-1
-1

样例输出 Copy

Sample 1:
The new list A:1 2 3 4 5 6 7 8 9 10 11
The new list B:3 6

Sample 2:
There is no item in A list.
There is no item in B list.
/* creat the linked list and add b to a
@author: CangCheng
@date: 17/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);

int PrintEmpty(struct node *headPtrA, struct node *headPtrB);

void printList(struct node *headPtr);

void addBtoA(struct node *headPtrA, struct node *headPtrB);

void PrintAB(struct node *headPtrA, struct node *headPtrB);

int main() {
    struct node *headPtrA = NULL, *headPtrB = NULL;
    headPtrA = creatLinkedList(headPtrA);
    headPtrB = creatLinkedList(headPtrB);
    addBtoA(headPtrA, headPtrB);
    PrintAB(headPtrA, headPtrB);
    delLinkedList(headPtrA);
    delLinkedList(headPtrB);
    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;
    }
}

//如果有空链表的输出
int PrintEmpty(struct node *headPtrA, struct node *headPtrB) {
    headPtrA = headPtrA->nextPtr;
    headPtrB = headPtrB->nextPtr;
    if (headPtrA == NULL && headPtrB != NULL) {
        printf("There is no item in A list.\n");
        printf("The new list B:");
        printList(headPtrB);
        return 1;
    }
    if (headPtrA != NULL && headPtrB == NULL) {
        printf("The new list A:");
        printList(headPtrA);
        printf("There is no item in B list.\n");
        return 1;
    }
    if (headPtrA == NULL && headPtrB == NULL) {
        printf("There is no item in A list.\n");
        printf("There is no item in B list.\n");
        return 1;
    }
    return 0;
}

//将B归并到A
void addBtoA(struct node *headPtrA, struct node *headPtrB) {
    struct node *thisPtrA, *thisPtrB, *lastPtrA, *lastPtrB;
    lastPtrA = headPtrA;
    lastPtrB = headPtrB;
    thisPtrA = lastPtrA->nextPtr;
    thisPtrB = lastPtrB->nextPtr;
    while (thisPtrB != NULL) {
        if (thisPtrA != NULL && thisPtrB->data > thisPtrA->data) {
            thisPtrA = thisPtrA->nextPtr;
            lastPtrA = lastPtrA->nextPtr;
            continue;
        }
        if (thisPtrA != NULL && thisPtrB->data == thisPtrA->data) {
            thisPtrB = thisPtrB->nextPtr;
            lastPtrB = lastPtrB->nextPtr;
            continue;
        }
        if (thisPtrA != NULL && thisPtrB->data < thisPtrA->data) {
            lastPtrB->nextPtr = thisPtrB->nextPtr;
            lastPtrA->nextPtr = thisPtrB;
            thisPtrB->nextPtr = thisPtrA;
            lastPtrA = lastPtrA->nextPtr;
            thisPtrB = lastPtrB->nextPtr;
            continue;
        }
        if (thisPtrA == NULL) {
            lastPtrA->nextPtr = thisPtrB;
            lastPtrB->nextPtr = NULL;
            break;
        }
    }
}

//打印AB链表
void PrintAB(struct node *headPtrA, struct node *headPtrB) {
    if (headPtrA->nextPtr == NULL || headPtrB->nextPtr == NULL) {
        PrintEmpty(headPtrA, headPtrB);
    } else {
        printf("The new list A:");
        printList(headPtrA->nextPtr);
        printf("The new list B:");
        printList(headPtrB->nextPtr);
    }
}

//打印链表
void printList(struct node *headPtr) {
    struct node *thisPtr;
    thisPtr = headPtr;
    while (thisPtr != NULL) {
        if (thisPtr->nextPtr != NULL) {
            printf("%d ", thisPtr->data);
        } else if (thisPtr->nextPtr == NULL) {
            printf("%d\n", thisPtr->data);
        }
        thisPtr = thisPtr->nextPtr;
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值