【问题标题】:Doubly linked list not printing values properly双向链表未正确打印值
【发布时间】:2019-10-06 22:06:22
【问题描述】:

当我调用方法来打印存储在我的双向链表的节点中的数据时,除了空字符串和 0 之外什么都不会打印

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

struct node {
    int weight;
    string name;
    node *nextname, *nextweight;
};
node *namehead = NULL, *weighthead = NULL;

bool isEmpty()
{
    if (namehead == NULL && weighthead == NULL)
        return true;
    else
        return false;
}

void addperson(int w, string n)
{
    node* newNode = new node;
    node *prev, *curr = newNode;
    if (isEmpty()) {
        namehead = newNode;
        weighthead = newNode;
    }
    else {
        curr = prev = namehead;
        if (curr->name > n) {
            namehead = newNode;
            newNode->nextname = curr;
        }
        else {
            do {
                if (curr->name <= n) {
                    prev = curr;
                    curr = curr->nextname;
                }
                else
                    break;
            } while (curr != NULL);
            prev->nextname = newNode;
            newNode->nextname = curr;
        }
        curr = prev = weighthead;
        if (curr->weight > w) {
            weighthead = newNode;
            newNode->nextweight = curr;
        }
        else {
            do {
                if (curr->weight <= w) {
                    prev = curr;
                    curr = curr->nextweight;
                }
                else
                    break;
            } while (curr != NULL);
            prev->nextweight = newNode;
            newNode->nextweight = curr;
        }
    }
}

void printname()
{
    node* curr = namehead;
    do {
        cout << curr->name << " - " << curr->weight << endl;
        curr = curr->nextname;
    } while (curr != NULL);
    cout << endl;
}

void printweight()
{
    node* curr = weighthead;
    do {
        cout << curr->name << " - " << curr->weight << endl;
        curr = curr->nextweight;
    } while (curr != NULL);
    cout << endl;
}

int main()
{
    int w = 0;
    string n;
    for (int i = 0; i < 15; i++) {
        cout << "Enter weight: ";
        cin >> w;
        if (w == -1)
            break;
        cout << "Enter name: ";
        cin >> n;
        addperson(w, n);
    }
    printname();
    printweight();
    return 0;
}

预期输出(按名称):

约翰 - 220

史蒂文 - 190

泰勒 - 150

预期产量(按重量计):

泰勒 - 150

史蒂文 - 190

约翰 - 220

电流输出(双向):

" " - 0

" " - 0

" " - 0

编辑 通过采纳 cmets 中关于在 add 方法中将值 w(权重)和 n(名称)实际分配给临时节点的建议,问题已得到解决。谢谢你的帮助。

curr->weight=w;
curr->name=n;

【问题讨论】:

  • void addperson(int w, string n) 中有node *newNode = new node; 然后node *prev, *curr = new node; 为什么要为一个人分配2 个节点?如果列表不为空,则以下行会泄漏为 curr 分配的节点:curr=prev=namehead;
  • 先尝试实现int的链表,然后再尝试更复杂数据类型的链表。
  • 节点的权重值在哪里设置为add()中作为参数传递的权重值?可能是其中的一部分。
  • @Chipster 指出了第二个错误。您忘记设置新节点的weightname
  • addPerson 中的缩进非常混乱

标签: c++ pointers data-structures doubly-linked-list


【解决方案1】:

在 add 方法中将传递的值分配给权重和名称成员到占位符节点:

curr->weight=w;
curr->name=n;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2021-07-24
    相关资源
    最近更新 更多