c++ - 链接列表程序在收到用户的值后崩溃

标签 c++ memory-leaks crash linked-list command-prompt

我的程序旨在运行多个函数,insertnode 从用户那里获取值并使用节点创建它们的列表并按从小到大的顺序对它们进行排序,printlist 打印由空格分隔的值,mergelist 将两个列表合并为order 和 reverselist 反转列表。命令提示符接受值,但一旦为第二个列表输入停止条件 (0),它就会崩溃。 Visual Studio 未显示任何错误。我认为函数或指针有问题。有人告诉我内存泄漏,但我不确定如何解决。

#include <iostream>
#include <stack>
using namespace std;

class node {
private:
    double num;
    node *link;
public:
    node() { }
    node(double m, node *n) { num = m; link = n; }
    node* getlink() { return link; }
    double getdata() { return num; }
    void setdata(double m) { num = m; }
    void setlink(node* n) { link = n; }
};

typedef node* nodeptr;

void insertnode(nodeptr& head, double m);
void printlist(nodeptr head);
nodeptr mergelists(nodeptr& head1, nodeptr& head2);
void reverselist(nodeptr& head);

int main()
{
    double input;a
    nodeptr head1 = NULL;       // Pointer to the head of List #1
    nodeptr head2 = NULL;       // Pointer to the head of List #2

    nodeptr temp;

    // Part 1 - Create two sorted lists

    cout << "-------------------------------------" << endl;
    cout << "CREATE LIST #1: " << endl;
    cout << "-------------------------------------" << endl;
    do {
        cout << "Enter value (0 to quit): ";
        cin >> input;

        insertnode(head1, input);

    } while (input != 0);

    cout << "-------------------------------------" << endl;
    cout << "CREATE LIST #2: " << endl;
    cout << "-------------------------------------" << endl;
    do {
        cout << "Enter value (0 to quit): ";
        cin >> input;

        insertnode(head2, input);

    } while (input != 0);

    // Part 1 - Print the lists to make sure that they are correct.
    printlist(head1);
    printlist(head2);
    // Part 2 - Merge the two lists and display the new merged list
    temp = mergelists(head1, head2);
    printlist(temp);
    // Part 3 - Reverse the merged list and then display it
    reverselist(temp);
    printlist(temp);


    return 0;
}

void insertnode(nodeptr& head, double m){
    nodeptr p = head;
    nodeptr k = p;
    if (!p){
        nodeptr n = new node(m, NULL);
    }
    else {
        while (m >= p->getdata()){
            k = p;
            p = p->getlink();
        }
        nodeptr n = new node;
        n->setdata(m);
        k->setlink(n);
        if (p){
            n->setlink(p);
        }

    }

}

void printlist(nodeptr head){
    nodeptr p = head;
    while (p){
        double m = p->getdata();
        cout << m << " ";
        p = p->getlink();
    }
    cout << endl;
}

nodeptr mergelists(nodeptr &head1, nodeptr &head2){
    nodeptr result = 0, last = 0;;
    if (head1->getdata() <= head2->getdata()){
        result = head1;
        head1 = head1->getlink();
    }
    else {
        result = head2;
        head2 = head2->getlink();
    }
    last = result;
    while (head1 && head2){
        if (head1->getdata() <= head2->getdata()){
            last->setlink(head1);
            last = head1;
            head1 = head1->getlink();
        }
        else{
            last->setlink(head2);
            last = head2;
            head2 = head2->getlink();
        }
    }
    if (head1) 
        last->setlink(head1);
    else if (head2) 
        last->setlink(head2);
    last = 0;
    head1 = 0;
    head2 = 0;
    return result;
}

void reverselist(nodeptr& head){
    stack<double> holder;
    nodeptr p = head;
    while (p){
        holder.push(p->getdata());
        p = p->getlink();
    }
    p = head;
    while (p){
        p->setdata(holder.top());
        holder.pop();
        p = p->getlink();
    }
}

最佳答案

这个方法有几个问题:

void insertnode(nodeptr& head, double m){
    nodeptr p = head;
    nodeptr k = p;

    if (!p)
    {
        head = new node(m, NULL); // Update head
    }
    else 
    {
        while (p && m >= p->getdata()) // Check for p!=NULL
        {
            k = p;
            p = p->getlink();
        }
        nodeptr n = new node;
        n->setdata(m);
        k->setlink(n);
        if (p)
        {
            n->setlink(p);
        }
    }
}

简化版:

void insertnode(nodeptr& head, double m)
{
    nodeptr p = head;
    nodeptr k = nullptr;

    while (p && m >= p->getdata())
    {
        k = p;
        p = p->getlink();
    }

    if (!k)
    {
        head = new node(m, p);
    }
    else
    {
        k->setlink(new node(m, p));
    }
}

关于c++ - 链接列表程序在收到用户的值后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27392404/

相关文章:

PreparedStatement 中的 Java 资源泄漏

c++ - 单击两次 listWidget 项目时 Qt 崩溃

c++ - 更改方法的保护级别是否被认为是好的做法?

c++ - udp 选择超时问题。超时或从所有客户端读取

c++ - 如何正确释放返回值

java - 尝试将值插入 sqlite 数据库表时应用程序崩溃

ios - 我在 iOS8 中使用了 uialertview ,但是在我两次单击取消按钮后应用程序崩溃了

c++ - 如何在 centos 中使用更新选项?

c++ - ZeroMQ:如何将 Poller 中使用的 pollitem_t 项转换回 ZeroMQ 套接字?

ios - 内存管理 : does this code has a memory leak?