我想我可能做对了,headByRating 和 headByName 都引用相同的地址。
我整天都在画图表尝试新事物等,但我并没有真正取得任何进展。
我有两个列表指针,headByRating 和 headByName。
以及两个节点指针 nextByName 和 nextByRating。
不知何故,我需要能够按名称和评级对这些内容进行排序。
我一直在想我是通过每个 ptr 的地址来做到这一点的。
以我一直在尝试排序的2条语句为例:
//main.cpp
list *wineries = new list();
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));
wineries->insert(winery("Gallo", "Napa Valley", 200, 25));
酒厂 ctor 很好,此时所有内容都已分配并进入对象:
//list.cpp
void 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.h
struct node
{
winery item;
node * nextByName;
node * nextByRating;
};
class list
{
...
private:
node * headByName;
node * headByRating;
};
对此有什么好的方法吗?我认为我这样做不对。