1
我正在嘗試創建一個鏈接列表,該鏈接列表按名稱和評分排序。評分排序部分按正確順序排列所有內容,但出於某種原因,按字母順序排序不會以正確的順序(甚至以相反的順序)放置任何內容。有時,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;
}
我這裏還有的酒廠類提供給我的功能:
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; }