【问题标题】:Printing nodes smoothly from linkedlist从链表顺利打印节点
【发布时间】:2015-01-09 19:36:09
【问题描述】:

我正在将文本文件中的信息添加到节点中,然后创建一个链接列表,然后将其打印出来,但我遇到了问题。我打印了节点,结果很完美,但是当我将它添加到列表并打印出列表时,我会不断重复,并且需要大约 6 个小时才能完成列表,而它最多需要 20 秒,它最终遍历列表中的信息,但在继续之前重复某些信息大约 500 次,同时重复所有先前的信息相同的次数。这是我的addprint 函数:

void customerlist::add(customer* ustomer)
{
    customer* p = new customer;
    p = ustomer;
    p->next = NULL;
    if (head != 0)
    {
        curr = head;
        while (curr->next != NULL)
        {
            curr = curr->next;
        }
        curr->next = p;
        n++;
    }
    else
    {
        head = p;
    }
}

int customerlist::getLength()
{
    return n;
}

void customerlist::print()
{
    curr = head;
    while (curr != NULL)
    {
        cout << curr->ID << " ";
        cout << curr->name << "  " << curr->lastname << "  " << curr->town << endl;
        curr = curr->next;
    }
}

我的主要:

while (!in.eof())
{
    account* bank = new account;
    customer* ustomer; in >> ustomer->ID;
    // display number of customers
    if (ustomer->ID < 90000000)
    {
        count++;
        in >> ustomer->name >> ustomer->lastname >> ustomer->town;
        // cout << ustomer->ID<< " " << ustomer->name << "  " << ustomer->lastname<< "        "         << ustomer->town << endl ;
        ustomerlist.add(ustomer);
        ustomerlist.print();
    }
    else
    {
        break;
    }
}

【问题讨论】:

  • 请提出一个可以回答的问题。

标签: c++ visual-c++ visual-studio-2012 linked-list nodes


【解决方案1】:

每次向其中添加元素时,您都会打印出整个列表。所以你实际上是在打印#elements 阶乘线。将 customerlist.print() 移到休息前。

编辑 - 正如其他发帖人所指出的,打印问题远不是代码中最重要的问题,但上述更改应该可以解决它。

【讨论】:

  • 非常感谢! :)
【解决方案2】:

好的,让我们列出一些直接的问题:

add 函数中分配内存并将其分配给p,然后您直接重新分配p 以指向ustomer 所指向的位置,从而使您失去分配的内存。

main 函数中你不应该使用while(!in.eof()),因为eofbit 标志不会设置,直到你尝试从文件之外读取你将迭代一次到多次。相反,例如

while (in >> name >> lastname >> town) { ... }

然后你遇到了最糟糕的问题:Undefined behavior,因为你有指针 ustomer,但你从来没有初始化那个指针,你从来没有让它指向任何地方。

最后一个问题的解决方案也可以解决第一个问题(内存泄漏):不要在add 函数中分配阳极,而是在循环中分配一个节点并在add 函数中使用它。

【讨论】:

    【解决方案3】:

    您提到打印功能在“继续”之前会“重复”信息。原因如下:

    当你添加更多节点时,所有之前的节点都会被打印出来,所以当你添加第 N 个节点时,你将打印 (N^2)/2 个项目,第 M 个节点被重复 N-M 次(它是二次而不是阶乘)。因此,当您有五个客户说 A B C D E 时,您将看到:

    A 
    A B 
    A B C 
    A B C D 
    A B C D E 
    

    相反,每次添加新节点时,打印该新节点而不是整个列表。

    在你的 main 中考虑这个逻辑:

    main(){
        count = 0;
        tail = head;
        while (!in.eof())
        {
            customer *new_customer;
            in >>  new_customer->ID;
            //check ID against defined MAX instead of a hard coded #
            if(new_customer->ID < MAX_ID) {
                count ++;
                in >> new_customer->name >> new_customer->lastname >> new_customer->town;
    
                tail->next = new_customer;
                tail = tail->next;
                tail->next = NULL;
                // here, print each new node using cout, so you will have the complete list as you add
                cout << new_customer->name << " " << blah blah << endl;
    
                // unless there's a specific need to print the entire list every time, use:  
                // customerlist.print(); 
            }
            else {
                break;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-02-25
      • 2013-05-10
      • 2017-04-22
      • 2020-03-17
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2021-07-12
      • 2020-01-04
      相关资源
      最近更新 更多