我正在尝试创建一个按名称和评级排序的链接列表。评级排序部分按顺序排列所有内容,但由于某种原因,字母排序不会按正确的顺序排列任何内容(或者甚至以相反的顺序排列)。有时,List :: Insert的字母排序部分会破坏printByRating()函数。有人请帮忙吗?
这是我的插入功能:
void List::insert(const Winery& winery)
{
Node * prev = NULL;
Node * curr = NULL;
Node * newNode = new Node(winery);
newNode->nextByName = NULL;
newNode->nextByRating = NULL;
//sort by name
curr = headByName;
while (curr!=NULL && curr->item.getName() < newNode->item.getName())
{
prev = curr;
curr = curr->nextByName;
}
newNode->nextByName = curr;
if(prev == NULL)
headByName = newNode;
else
prev->nextByName = newNode;
//sort by rating
curr = headByRating;
while (curr!=NULL && curr->item.getRating() > newNode->item.getRating())
{
prev = curr;
curr = curr->nextByRating;
}
newNode->nextByRating = curr;
if(prev == NULL)
headByRating = newNode;
else
prev->nextByRating = newNode;
}
以下是我在Winery课程中可以使用的功能:
const char * const getName() const { return name; }
const char * const getLocation() const { return location; }
const int getAcres() const { return acres; }
const int getRating() const { return rating; }
答案 0 :(得分:1)
您似乎在比较const char*
指针而不是比较它们的内容。使用strcmp
或std::string
按字典顺序比较字符串。