字母链表顺序不对



我正在尝试创建一个由名称和评级排序的链表。排序部分将所有内容按正确顺序排列,但由于某种原因,字母排序不能按正确顺序排列任何内容(甚至颠倒顺序)。有时,List::Insert的字母排序部分会破坏printByRating()函数。有人来帮忙吗?

下面是我的insert函数:
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; }

似乎你正在比较const char*指针,而不是比较它们的内容。使用strcmpstd::string按字典顺序比较字符串

相关内容

  • 没有找到相关文章

最新更新