gpt4 book ai didi

c++ - 链表部分

转载 作者:行者123 更新时间:2023-11-27 23:34:54 24 4
gpt4 key购买 nike

我想我可能做对了,headByRating 和 headByName 都指的是同一个地址。

我整天都在画斜方图,尝试新事物等,但我没有取得任何进展。

我有两个列表指针,headByRating 和 headByName。和两个节点指针 nextByName 和 nextByRating。

不知何故,我需要能够按名称和评级对这些内容进行排序。我一直在想,我是通过每个 ptrs 的地址来做到这一点的。

以我一直试图排序的 2 个语句为例:

//main.cpplist *wineries = new list();wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));wineries->insert(winery("Gallo", "Napa Valley", 200, 25));

winery ctor 很好,此时一切都已分配并放入对象中:

//list.cppvoid list::insert( const winery& winery ){         list *listPtr  = new list(); // havent really used the list obj. yet.    node *current = new node( winery ); // winery is now a node.    node *temp    = current;     // temp knows about the nodes address.    while ( temp->nextByName != NULL )    {                 // check for null and reassign         temp = temp->nextByName;    }    node *new_node = new node( winery ); // creating a new node.    new_node->item = winery;     new_node->nextByName   = new_node;    new_node->nextByRating = new_node;}
// list.hstruct node{    winery  item;    node *  nextByName;    node *  nextByRating;};class list{    ...private:    node * headByName;    node * headByRating;};

什么是解决这个问题的好方法?我不认为我这样做是对的。

最佳答案

为什么要滚动自己的链表?为什么不使用 std::list?如果是为了双重排序,两个单独的指针列表就可以了,如果您可以使用提供的容器类,那就简单多了。

或者,如果您需要对其进行排序,链表是否是最佳选择? std::vector 通常更易于排序,或者 std::set 自行维护顺序。

关于c++ - 链表部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1309960/

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