gpt4 book ai didi

c++ - 我正在创建一个双向链表,它将按字母顺序排列名称列表,但我不确定要在 int main() 函数中放入什么

转载 作者:行者123 更新时间:2023-11-28 08:14:37 25 4
gpt4 key购买 nike

最终,该程序将按字母顺序打印出一个名称列表,以及与该名称关联的其他属性。换句话说,输出屏幕将显示如下:

Ares: Greek, fire, sword.
Freia: Norse, water, bow and arrow.
Poseidon: Greek, horses, ocean.
Thor: Norse, chariot, hammer.
Zeus: Greek, cloud, lightning.

同样,名字在此列表中按字母顺序排列,但属性与它们并排打印。关于我的 int main(),我不确定我应该如何开始对这些名称进行排序并按顺序排列它们。我有一个必须排序的未排序列表(使用将这些名称添加/插入到正确顺序的函数)。

    //
// This is a standard library support code to the chapters of the book
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#ifndef STD_LIB_FACILITIES_GUARD
#define STD_LIB_FACILITIES_GUARD 1

#include <cmath>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <string>

using namespace std;

//------------------------------------------------------------------------------

// Helper function to show an error message
inline void error(const string& errormessage)
{
throw runtime_error(errormessage);
}

//------------------------------------------------------------------------------

#endif // STD_LIB_FACILITIES_GUARD

//------------------------------------------------------------------------------

struct Link {
string name;
string mythology;
string vehicle;
string weapon;

Link* prev;
Link* succ;
Link(const string& n, const string& a, const string& b, const string&c,Link* p = 0,
Link* s = 0)
: name(n), mythology(a), vehicle(b), weapon(c), prev(p), succ(s) { }
};



Link* insert(Link* p, Link* n) // insert n before p; return n
{
if (n==0) return p;
if (p==0) return n;
n->succ = p; // p comes after n
if (p->prev) p->prev->succ = n;
n->prev = p->prev; // p's predecessor becomes n's predecessor
p->prev = n; // n becomes p's predecessor
return n;
}

void print_all(Link *p)
{
Link *current;
current = p;
while(current)
{
cout<<"For this link we have: \n";
cout<<"Name: "<<current->name<<".\n";
cout<<"Info1: "<<current->mythology<<".\n";
cout<<"Info2: "<<current->vehicle<<".\n";
cout<<"Info3: "<<current->weapon<<".\n";
current = current->succ;
}

}
Link * add_after_find(Link *p, Link *n,const string& s )
{ Link *current = 0;
current = p;
/* empty list */
if(p == 0)
{ cout<<"List is empty so string not found so not added after it. \n";

return 0;
}
/* DO WE NEED ONE LINK ONLY */
else if(p->succ == 0) /* one link only */
{
if(p->name == s)
{

/* add after link with s */
/* p in front */
p->succ = n;
n->prev = p;
p->prev = 0;
n->succ = 0;

return p;
} /* end of if names = */
else {
cout<<"String not found in link listed so not added. \n";
return p;

}

} /* end of one link */

else /* two or more links */

{
current = p;
while(current->succ)
{
if (s == current->name)


{

/* then n goes AFTER this link */
n->prev = current;
n->succ = current->succ;
current->succ = n;


return p;
} /* end of name matches */

else
{
current = current->succ;
}
}// end of while
/* if outside of while then we are at last link with a current -> name
so s not found */
cout<<"String is not found so not add after it. \n";
return p;
} // end of else 2 or more
} // end of function

int main()
{
Link*newlist = new Link("Thor","Norse","chariot","hammer");
newlist = add_after_find(newlist,new Link("Hera","Greek", "horse", "arrow"),"Thor");
newlist = add_after_find(newlist,new Link("Poseidon","Greek", "ocean", "trident"),"Freia");
newlist = add_after_find(newlist,new Link("Ares","Greek", "fire", "sword"),"Poseidon");
newlist = add_after_find(newlist,new Link("Zeus","Greek", "cloud", "lightning"),"Ares");

print_all(newlist);
cout<<"Now let's alphabetize these five gods.\n";
system("Pause");
return 0;
}

最佳答案

我假设这是家庭作业,如果不是,简单的答案是您应该在该容器中使用 std::listsort 方法。

main 中放什么?最有可能的是:sort_list(newlist),其中指针通过引用传递(因为列表的头部可能会改变)。至于如何实现它,这取决于您要实现的排序算法,最简单的可能是冒泡排序,列表的下一个最佳选择是合并排序。谷歌他们,如果你需要算法方面的帮助,回来问。

与此同时,您可能想要解决我提出的作为对问题的评论的问题:代码格式、内存泄漏(如果未找到位置则在插入时和程序结束时)、正确性数据结构始终......我没有做过深入的分析,但我觉得你的 add_after_find 需要在列表的尾部添加元素时可能会失败.. . 在你开始甚至考虑排序之前,你应该确保输入是正确的。与开始添加更多代码相比,调试当前问题更容易。

关于c++ - 我正在创建一个双向链表,它将按字母顺序排列名称列表,但我不确定要在 int main() 函数中放入什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8076586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com