按字母顺序排列的双向链表中的链接

时间:2018-03-15 04:22:38

标签: c++ class linked-list

我一直遇到一些麻烦,让我的程序在链表中插入给定的名称及其权重,链表中有相应的名称和权重链接,这两个链接都按升序/字母顺序排序。权重链接工作正常,但我似乎无法确定我的名字链接器出错了什么。任何帮助将不胜感激。问题最可能在于insertName()私有函数。

#include <iostream>
#include <string>

using namespace std;

class DoublyLinkedList
{
public:
    void insert(string name, double weight);
    void printNameAsc();
    void printWeightAsc();

private:
    struct Node
    {
        string name;
        double weight;
        Node* nextName;
        Node* nextWeight;
    };
    Node* nameHead = NULL;
    Node* weightHead = NULL;
    Node* newP = NULL;

    void insertName();
    void insertWeight();
};
void DoublyLinkedList::insert(string name, double weight)
{
    // variable declaration
    newP = new Node;
    newP->name = name;
    newP->weight = weight;
    newP->nextName = NULL;
    newP->nextWeight = NULL;

    // empty first element check
    if (nameHead == NULL && weightHead == NULL)
    {
        nameHead = newP;
        weightHead = newP;
        return;
    }

    // name and weight insertion
    insertName();
    insertWeight();

    return;
}
void DoublyLinkedList::insertName()
{
    Node* activeP = nameHead;
    Node* prevP = NULL;

    // traversing through name links
    while (true)
    {
        if (activeP == NULL)
        {
            break;
        }

        if ((activeP->name).compare(newP->name))
        {
            break;
        }

        prevP = activeP;
        activeP = activeP->nextName;
    }

    //insertion
    newP->nextName = activeP;
    if (activeP == nameHead)
    {
        nameHead = newP;
    }
    else
    {
        prevP->nextName = newP;
    }

    return;
}
void DoublyLinkedList::insertWeight()
{
    Node* activeP = weightHead;
    Node* prevP = NULL;

    // traversing through weight links
    while (true)
    {
        if (activeP == NULL)
        {
            break;
        }

        if (newP->weight < activeP->weight)
        {
            break;
        }

        prevP = activeP;
        activeP = activeP->nextWeight;
    }

    //insertion
    newP->nextWeight = activeP;
    if (activeP == weightHead)
    {
        weightHead = newP;
    }
    else
    {
        prevP->nextWeight = newP;
    }

    return;
}
void DoublyLinkedList::printNameAsc()
{
    Node* activeP = nameHead;

    while (activeP != NULL)
    {
        cout << activeP->name << " " << activeP->weight << endl;
        activeP = activeP->nextName;
    }

    return;
}
void DoublyLinkedList::printWeightAsc()
{
    Node* activeP = weightHead;

    while (activeP != NULL)
    {
        cout << activeP->name << " " << activeP->weight << endl;
        activeP = activeP->nextWeight;
    }

    return;
}

int main()
{
    DoublyLinkedList nameList;

    nameList.insert("Michael", 275);
    nameList.insert("Tom", 150);
    nameList.insert("Abe", 200);

    nameList.printNameAsc();
    system("pause");
    nameList.printWeightAsc();
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

很确定你的问题在这里:

    if ((activeP->name).compare(newP->name) > 0)
    {
        break;
    }

看起来您正在使用activeP找到插入点。当activeP-&gt; name&gt; newP-&gt;名称,您要停止搜索。

if ((activeP->name).compare(newP->name))

无效,如果activeP&lt; newP(比较语句为-1)或activeP&gt; newP(比较语句为1),实际上只跳过具有相同名称的人。

记住,从C继承,真理被定义为非虚假,定义为(!0)

另外,为什么不直接将newP的引用传递给insertName()和insertWeight()?最好不要将(可能悬空)指针存储为成员变量。