为什么一个链表的结束节点从 NULL 变为另一个链表的下一个节点?

Why did one linked list's end node change from NULL to another list's next node?

第一个函数linkAndMove用于基本链接和移动点处理。

Union函数用于查找链表lalb中的所有数字(无重复)

我的测试例子:la {1,3}lb{3,5}

但是最后当la指向NULL,而lb指向5.

在第一个函数 linkAndMove 之后,列表 la 更改为 {1,3,5}

为什么 la 的结束节点从 NULL 变为 lb 现在的节点 5?

before first function

after first function

    void linkAndMove(slink **pNode, slink **qNode, slink **finNode,
                 int linkFlag, int moveFlag) {

    if (linkFlag == -1 || moveFlag == -1) {
        cout << "ERROR! No matched logical in basic link list process." << endl;
        exit(1);
    }

    switch (linkFlag) {
        case 0:
            if ((*finNode)->data != (*pNode)->data) {
                (*finNode)->next = (slink *) malloc(sizeof(MemLEN));
                (*finNode)->next = (*pNode);
                (*finNode) = (*finNode)->next;
            }
            break;
        case 1:
            if ((*finNode)->data != (*qNode)->data) {
                (*finNode)->next = (slink *) malloc(sizeof(MemLEN));
                (*finNode)->next = (*qNode);
                (*finNode) = (*finNode)->next;
            }
            break;
        case 2:
            break;
        default:
            cout << "ERROR! No matched logical in basic link list process." << endl;
            exit(1);
    }

    switch (moveFlag) {
        case 0:
            (*pNode) = (*pNode)->next;
            break;
        case 1:
            (*qNode) = (*qNode)->next;
            break;
        case 2:
            (*pNode) = (*pNode)->next;
            (*qNode) = (*qNode)->next;
            break;
        default:
            cout << "ERROR! No matched logical in basic link list process." << endl;
            exit(1);
    }
}

void Union(slink *la, slink *lb, slink *lc) {  
    slink *pNode, *qNode;

    pNode = la->next;
    qNode = lb->next;

    int linkFlag, moveFlag;

    while (pNode != NULL || qNode != NULL) {
        linkFlag = -1;
        moveFlag = -1;

        if (pNode == NULL) {
            linkFlag = moveFlag = 1;
        } else if (qNode == NULL) {
            linkFlag = moveFlag = 0;
        } else {
            if (pNode->data > qNode->data) {
                linkFlag = 1;
                moveFlag = 1;
            } else if (pNode->data < qNode->data) {
                linkFlag = 0;
                moveFlag = 0;
            } else {
                linkFlag = 0;
                moveFlag = 2;
            }
        }

        /*if (pNode == NULL) {
            linkAndMove(NULL, &qNode, &lc, linkFlag, moveFlag);
        } else*/
        linkAndMove(&pNode, &qNode, &lc, linkFlag, moveFlag);
    }
}

找到原因了

因为在函数linkAndMove中,指针finNode连接到列表la的节点。在前面的代码中,使用节点的 next 连接 pNode,因此将 la 的结束节点从 NULL 更改为该节点。

我找到的解决方案是为列表 lc 创建新节点,它不会感染原始数据列表 la。代码在这里。

  switch (linkFlag) {
    case 0:
        if ((*finNode)->data != (*pNode)->data) {  
            (*finNode)->next = initLinkNode();
            (*finNode) = (*finNode)->next;
            (*finNode)->data = (*pNode)->data;
        }
        break;
    case 1:
        if ((*finNode)->data != (*qNode)->data) {
            (*finNode)->next = initLinkNode();
            (*finNode) = (*finNode)->next;
            (*finNode)->data = (*qNode)->data;
        }
        break;
    case 2:
        break;
    default:
        cout << "ERROR! No matched logical in basic link list process." << endl;
        exit(1);
}