首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在双链接列表中搜索多次创建成员。

在双链接列表中搜索多次创建成员。
EN

Stack Overflow用户
提问于 2021-07-06 16:00:19
回答 1查看 70关注 0票数 0

我正在研究PPPC++的练习,我有一个List类,它包含多个神及其属性。

例:{ Thor,Norse,Chariot,Mjolnir}或{ Hera,希腊语,战车,石榴}其中Thor是挪威的神,Hera是希腊的神。

我正在编写代码,以找到指向所有希腊神的指针。

我不明白为什么赫拉被发现两次,阿瑞斯被发现4次。怎么了?谢谢!

代码语言:javascript
复制
found 0x5586b65353f0 Poseidon
found 0x5586b6535350 Athena
found 0x5586b65351d0 Hera
found 0x5586b65351d0 Hera
found 0x5586b6534f50 Ares
found 0x5586b6534f50 Ares
found 0x5586b6534f50 Ares
found 0x5586b6534f50 Ares
found 0x5586b6534eb0 Zeus

守则是:

代码语言:javascript
复制
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;

struct God
{
    God(const string &n, const string &m, const string &v, const string &w)
        : name{n}, mythology{m}, vehicle{v}, weapon{w} {}

    string name;
    string mythology;
    string vehicle;
    string weapon;
};

class Link
{
public:
    God god; 
    Link(const string &n, const string &m, const string &v, const string &w, Link *p = nullptr, Link *s = nullptr)
        : god{n, m, v, w}, prev{p}, succ{s} {}
    Link *insert(Link *n);                 // insert n before this object
    Link *find_mythology(const string &s); // find s in list

    Link *next() const { return succ; }
    Link *previous() const { return prev; }

private:
    Link *prev;
    Link *succ;
};

Link *Link::insert(Link *n) // insert n before this object; return n
{
    if (n == nullptr)
        return this;
    if (this == nullptr)
        return n;
    n->succ = this;     // this object comes after n
    if (prev)           // if prev of this (object) is not zero - meaning there's a Link object before this object (or p)
        prev->succ = n; // perv->succ
    n->prev = prev;     // this object’s predecessor becomes n’s predecessor
    prev = n;           // n becomes this object’s predecessor
    return n;           // returns n(the new element) which is before the top node
}
Link *Link::find_mythology(const string &s) // find s in list;
{
    Link *p = this;
    while (p)
    {
        if (p->god.mythology == s)
            return p;
        p = p->succ; // move to the next node
    }
    return nullptr; // return nullptr for “not found”
}
void print_all(Link *p)
{
    while (p)
    {
        cout << " " << p->god.name << ", " << p->god.mythology << ", " << p->god.vehicle << ", " << p->god.weapon;
        if (p = p->next()) // moved to the next node
            cout << "\n";
    }
}

int main()
{
    Link *all_gods = new Link{"Zeus", "Greek", "chair", "lightning"};
    all_gods = all_gods->insert(new Link{"Ares", "Greek", "wings", "sword"});
    all_gods = all_gods->insert(new Link{"Odin", "Norse", "Sleipner", "Gungnir"});
    all_gods = all_gods->insert(new Link{"Thor", "Norse", "Chariot", "Mjolnir"});
    all_gods = all_gods->insert(new Link{"Freia", "Norse", "chariot", "Brisingamen"});
    all_gods = all_gods->insert(new Link{"Hera", "Greek", "chariot", "pomegranate"});
    all_gods = all_gods->insert(new Link{"Tyr", "Norse", "chariot", "spear of justice"});
    all_gods = all_gods->insert(new Link{"Athena", "Greek", "chariot", "thunderbolt"});
    all_gods = all_gods->insert(new Link{"Poseidon", "Greek", "the sea", "trident"});

    print_all(all_gods); // cout the type of the 1st element
    cout << "\n\n";

//while (all_gods)
//{
//    Link *p = all_gods->find_mythology("Greek"); // this returns a pointer where it finds a Norse
//     cout << "found " << p << ' ' << p->god.name << '\n';
//   all_gods = all_gods->next();
//}

Link *p = all_gods;
while (p)
{
    if (p->god.mythology == "Greek")
        cout << "found " << p << ' ' << p->god.name << '\n';
    p = p->next();
}
delete[] p;
// here I will still use all_gods before deleting it below
delete all_gods;

print_all(all_gods);
cout << "\n\n";
print_all(p);
cout << "\n\n";
}

编辑

我在main()中更改了while循环,现在它做了我想做的事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-06 16:14:45

您的代码看起来很好(虽然不是很好)。

它似乎也像我所期望的那样运作:

代码语言:javascript
复制
// When you start the list is:
//    Poseidon : Athena : Tyr : Hera ......
while (all_gods)
{
    // So first time threw this loop you find: Poseidon
    Link *p = all_gods->find_mythology("Greek");
    cout << "found " << p << ' ' << p->god.name << '\n';

    // Now you are altering the list (and leaking an object.
    // but thats another question).
    //
    
    all_gods = all_gods->next();
    // But you dropped "Poseidon" off the front so the list is now:
    // Athena : Tyr : Hera ......
}

所以你第二次在循环中打印Athena。然后你把雅典娜从列表中删除,所以列表是Tyr : Hera ......

所以你的第三次循环,你会打印Hera (第一个希腊神)。但是,您删除了Tyr,所以列表是Hera ......

因此,第四次循环,您将再次打印Hera,因为她仍然在名单上,第一个希腊神。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68273856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档