【问题标题】:LinkedList sorting a-z链表排序 a-z
【发布时间】:2021-11-22 21:56:48
【问题描述】:

此函数的要求:如果全名(名字和姓氏)不等于当前列表中的任何全名,则添加它并返回true。应根据姓氏添加元素。具有相同姓氏的元素应根据 他们的名字。否则,不更改列表并返回 false(表示该名称已在列表中)。

//this function add nodes to the list
//return true if fullname isn't in the list. Else return false if fullname is in the list.
//should be added according to last name.
bool OnlineDating::makeMatch(const std::string& firstName, const std::string& lastName, const OnlineType& value)
{
    Node* p = head;
    //are these nodes already set to firstName and lastName in this function
    Node first;
    Node last;
    Node* temp = nullptr;
    //if the list is empty just insert the fullname and value to the list
    if (p == nullptr) {
        //add values to the empty list
        insertToRear(firstName, lastName, value);
        return true;
    }
    else {
        // so this loop is to check if fullname is in the list but first sort in alphebetial order
        //sure its added in alphebetical order
        //traverse the list after knowing where head is
        while (p != nullptr) {

            //checking to make sure theres at least another node in the list
            if (p->next != nullptr) {
                //its not going through ig loop?
                //these are used to check and alphebetically selected names
                if (p->last > p->next->last) {
                    insertToRear(p->first, p->last, p->value);
                    p->next = temp;

                    return true;
                }
                else if (p->next->last > p->last) {
                    insertToRear(p->first, p->last, p->value);
                    p->next = temp;

                    return true;
                }
                //check if full name is already in the list
                if (p->last == p->next->last) {
                    insertToRear(p->first, p->last, p->value);
                    p->next = temp;

                    return true;
                }
                else if (p->first > p->next->first) {
                    insertToRear(p->first, p->last, p->value);
                    p->next = temp;

                    return true;
                }
                else {
                    //returns false if it passes through these  checks
                    return false;
                }
            }
            p = p->next;
        }
    }
}

这是我的 main.cpp

int main()
{

    OnlineDating clippersGonnaClip;
    clippersGonnaClip.makeMatch("Kawhi", "Leonard", 2);
    clippersGonnaClip.makeMatch("Paul", "George", 13);
    clippersGonnaClip.makeMatch("Ivica", "Zubac", 40);
    clippersGonnaClip.makeMatch("Reggie", "Jackson", 1);
    clippersGonnaClip.makeMatch("Patrick", "Beverley", 21);
    for (int n = 0; n < clippersGonnaClip.howManyMatches(); n++) {
        string first;
        string last;
        int val;
        clippersGonnaClip.confirmMatch(n, first, last, val);
        cout << first << " " << last << " " << val << endl;
    }

    return 0;
}

老实说,我只想创建一个指向每个节点的 ptr。只要该节点不指向 nullptr 就进行检查,并按字母顺序对 LinkedList 进行排序。最后使用我的 *temp 将节点链接在一起。为什么每次编译时它都不让我通过 if 语句我得到一个负数?除了这个 makeMatch(...) 之外,其他所有函数都适用于这个程序。

【问题讨论】:

  • 开始更简单:首先创建您的列表并确保所有节点都在其中。然后创建一个函数来交换两个节点,并针对多种情况进行测试(两个第一个节点,两个最后一个节点,两个中间节点)。然后就可以开始看排序了。
  • 或者,你知道,使用标准 C++ 容器(最好是std::vector)和std::sort。
  • 列表不应该已经排序了吗?如果列表为空,您只使用firstName 和lastName?
  • 使用向量是有道理的,但我只能使用数据结构来完成这项任务,并且仅限于他提供的模板。艾伦,我希望列表按字母顺序排序。
  • 如果您只在正确位置插入排序列表,那么列表将始终被排序。对链表进行排序是一场噩梦,通常的做法是新建一个链表并使用插入排序

标签: c++ class append structure project


【解决方案1】:

要求中没有任何内容说明您需要使用的数据类型。在这种情况下,使用 std::set 最有意义。 通过提供对人物和集合的比较操作,您可以保证唯一性。像这样:

#include <set>
#include <string>

struct person_t
{
    std::string name;
    std::string last_name;
    unsigned int age;
};

bool operator<(const person_t& lhs, const person_t& rhs)
{
    if (lhs.last_name == rhs.last_name)
        return lhs.name < rhs.name;

    return lhs.last_name < rhs.last_name;
}

int main()
{
    // set will ensure all persons are unique
    std::set<person_t> persons;
    persons.insert({ "Kawhi", "Leonard", 2 });
    persons.insert({ "Paul", "George", 13 });
    persons.insert({ "Ivica", "Zubac", 40 });
    persons.insert({ "Reggie", "Jackson", 1 });
    persons.insert({ "Patrick", "Beverley", 21 });

}

【讨论】:

  • 我实际上不能使用全局结构,但是您检查列表的方式对我有帮助。谢谢你!
  • 好吧,我会让这个集合成为 OnlineDating 类的成员。然后该类中的所有函数都可以使用它。但这主要是为了向您展示许多数据结构已经在标准库中有一个(经过很好测试的)实现供您重用。
  • 是的,感谢您的评论!会调查的
猜你喜欢
  • 1970-01-01
  • 2021-10-06
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 2014-02-09
  • 2016-03-19
  • 1970-01-01
相关资源
最近更新 更多