coder12345678 2022-04-14 15:28 采纳率: 100%
浏览 37
已结题

链表的归并,归并后的链表前面总是多了两个空的结点

问题遇到的现象和发生背景

我编写一个将两个有序单链表归并为一个有序单链表的程序,但是归并后得到的单链表总是莫名其妙地在前面多了两个空的节点

问题相关代码,请勿粘贴截图
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

typedef struct LNode
{
    int data;
    struct LNode* next;
}LNode;

int main()
{
    void createlist(LNode*&, int);
    void mergelist(LNode*, LNode*, LNode*&);
    void printlist(LNode*);
    LNode* a, * b, * c;
    int n1, n2;

    cout << "Please enter the length of the first linked list:";
    cin >> n1;
    createlist(a, n1);
    printlist(a);
    cout << "Please enter the length of the second linked list:";
    cin >> n2;
    createlist(b, n2);
    printlist(b);
    mergelist(a, b, c);
    printlist(c);

    return 0;
}

void createlist(LNode*& a, int n)
{
    LNode* p;

    a = (LNode*)malloc(sizeof(LNode));
    p = (LNode*)malloc(sizeof(LNode));
    a->next = p;
    cout << "Please enter the elements of the linked list one by one in an increasing order:";
    for (int i = 0; i < n; i++)
    {
        cin >> p->data; 
        if (i < n - 1) 
        {
            p->next = (LNode*)malloc(sizeof(LNode));
            p = p->next;
        }
    }
    p->next = NULL;

    return;
}

void mergelist(LNode* a, LNode* b, LNode*& c)
{
    LNode* p;

    c = (LNode*)malloc(sizeof(LNode));
    p = (LNode*)malloc(sizeof(LNode));
    c->next = p;
    while (a != NULL || b != NULL)
    {
        if (a == NULL)
        {
            p->data = b->data;
            b = b->next;
        }
        else
        {
            if (b == NULL)
            {
                p->data = a->data;
                a = a->next;
            }
            else
            {
                if (a->data < b->data)
                {
                    p->data = a->data;
                    a = a->next;
                }
                else
                {
                    p->data = b->data;
                    b = b->next;
                }
            }
        }
        if (a != NULL || b != NULL)
        {
            p->next= (LNode*)malloc(sizeof(LNode));
            p = p->next;
        }
    }
    p->next = NULL;

    return;
}

void printlist(LNode* a)
{
    a = a->next;
    while (a)
    {
        cout << a->data;
        if (a->next)
        {
            cout << " -> ";
        }

        a = a->next;
    }
    cout << endl;
    

    return;
}

运行结果及报错内容

Please enter the length of the first linked list:8
Please enter the elements of the linked list one by one in an increasing order:1 3 5 9 12 15 16 21
1 -> 3 -> 5 -> 9 -> 12 -> 15 -> 16 -> 21
Please enter the length of the second linked list:8
Please enter the elements of the linked list one by one in an increasing order:3 6 8 9 10 15 19 24
3 -> 6 -> 8 -> 9 -> 10 -> 15 -> 19 -> 24
-842150451 -> -842150451 -> 1 -> 3 -> 3 -> 5 -> 6 -> 8 -> 9 -> 9 -> 10 -> 12 -> 15 -> 15 -> 16 -> 19 -> 21 -> 24

我想要达到的结果

能不能指点一下我的程序哪里出了问题?

  • 写回答

2条回答 默认 最新

  • coder12345678 2022-04-14 15:50
    关注

    我知道为什么了,多出来的两个结点是待归并链表的头结点

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月22日
  • 已采纳回答 4月14日
  • 创建了问题 4月14日

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度