[英]Search in a Doubly-Linked List founds members multiple times
我正在完成 PPPC++ 的練習,並且我有一個 List 類,其中包含多個具有屬性的神。
例如:{Thor, Norse, Chariot, Mjolnir} 或 {Hera, Greek, chariot, pomegranate} 其中托爾是北歐神,赫拉是希臘神。
我正在嘗試編寫代碼來找到指向所有希臘神的指針。
我不明白為什么赫拉會被發現兩次,而阿瑞斯會被發現 4 次。 怎么了? 謝謝!
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
代碼是:
#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 循環,現在它做了我想要的。
您的代碼看起來不錯(雖然不是很面向對象)。
它似乎也以我期望的方式工作:
// 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
。 然后您將 Athena 從列表中刪除,因此列表是Tyr : Hera ......
所以你第三次循環你將打印Hera
(希臘第一神)。 但是然后你放棄了Tyr
所以名單是Hera ......
因此,第四次循環時,您將再次打印Hera
,因為她仍然在列表中並且是第一位希臘神。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.