【问题标题】:C Sort Input By First Value and Second Value Into A Linked ListC按第一个值和第二个值将输入排序到一个链表中
【发布时间】:2013-04-07 22:07:52
【问题描述】:

过去几天我一直在为此苦苦挣扎。免责声明:这是一个家庭作业。我自己一直在努力,但我真的不知道我做错了什么。

我收到一份航空公司航班行李信息列表,格式为航空公司、目的地、行李车号。

我必须将此列表排序为一个新列表。新列表必须按航空公司排序,并且在每家航空公司内必须按目的地排序。

  • 新加坡巴尔的摩 176
  • ATA 阿伦敦 1549
  • 大陆桥港 915
  • 新加坡伯克利878
  • 新加坡利沃尼亚 1462
  • Quantas Fremont 1610

将整理为:

  • ATA 阿伦敦 1549
  • 大陆桥港 915
  • Quantas Fremont 1610
  • 新加坡巴尔的摩 176
  • 新加坡伯克利878
  • 新加坡利沃尼亚 1462

这是我现在正在使用的:

#Main.cpp#
int main()
{
    Node * head = NULL;

    ifstream fin("planes.txt");
    string airline, destination;
    int carNum;

    while(!fin.eof())
    {
        fin >> airline >> destination >> carNum;
        insertNode(airline, destination, carNum, head);
    }

    cout << endl;
    system("pause");
    return 0;
}



#Node.cpp#
void insertNode(string value1, string value2, int value3, Node *&head)
{
    Node * ptr = head;
    if (ptr == NULL || ptr->getString1() > value1)
    {
        insertAtHead(value1, value2, value3, head);
    }
    else
    {
        while (ptr->getNext() != NULL && ptr->getNext()->getString1() < value1)
        {
            ptr = ptr->getNext(); // advance to next node in list
        }
        insertValue(value1, value2, value3, ptr);
    }
}

void insertValue (string value1, string value2, int value,Node *afterMe)
{
    afterMe->setNext(new Node(value1, value2, value, afterMe->getNext()));
}

void insertAtHead (string value1, string value2, int value3,Node *&head)
{
    head = new Node(value1, value2, value3, head);
}

我将省略我的 node.h,因为它只包含函数声明和简单的访问器函数。

我当前的 insertNode 函数仅按第一个值排序。这是我的 insertNode 函数的一个变体,我尝试按第一个值和第二个值开始排序。

这是相当丑陋的。即使在笔记本纸上解决了几页之后,我仍然被所有这些混乱所困扰。

void insertNode(string value1, string value2, int value3, Node *&head)
{
    Node * ptr = head;
    if (ptr == NULL || ptr->getString1() > value1)
    {
        insertAtHead(value1, value2, value3, head);
    }
    else
    {
        while (ptr->getNext() != NULL)  //Keep going as long as I am not at the end of the list
        {
            if (ptr->getNext()->getString1() < value1) //If Airline in list is smaller then the Airline I am adding
            {
                ptr = ptr->getNext(); //Move onto the next value in the list
            }
            else if (ptr->getNext()->getString1() == value1) //If Airline in list is equivalent to the Airline I am adding
            {
                if(ptr->getNext()->getString2() < value2)   //The airlines matched up. How do the destinations compare?
                {
                    ptr = ptr->getNext(); //If the destination in the list is less than the one I am adding, move on
                }
                else
                {
                    insertValue(value1, value2, value3, ptr); //If one I am adding is not less than the list, add it here.
                }
            }
            else
            {
                insertValue(value1, value2, value3, ptr);
            }
        }
    }
}

【问题讨论】:

    标签: c++ list sorting linked-list


    【解决方案1】:

    在不知道您的确切要求的情况下,您似乎过于复杂了。如果您只是创建一个新字符串来保存 Airline + Destination + Baggage Car 并将其插入到排序列表中,您将实现数据目标。

    是否有任何理由必须将数据元素单独保存在列表行中?换句话说:

    string strToBeSorted = strAirline;
    strToBeSorted += " ";
    strToBeSorted += strDestination;
    strToBeSorted += " ";
    strToBeSorted += strBaggageCar;
    

    然后你插入strToBeSorted。这将为您提供一个可以进行字符串排序的字符串。这使得简单地对所有条目进行排序变得很简单。您将能够知道strToBeSorted 的去向,以及航空公司、目的地甚至行李车的去向。

    【讨论】:

    • 我不确定我是否理解。我必须阅读 Airline、Dest、Number 信息,然后将其添加到列表中,边走边排序。所以我在新加坡阅读了巴尔的摩,并将其添加到我的列表的开头。然后我读了 ATA - KCI。我看到 ATA 排在新加坡之前,所以我将 ATA 放在列表的首位,并将新加坡移到了一位。等等等等。我可以这样排序,但考虑到第二个值会让我失望。我真的迷路了,所以我希望我没有错误地阅读您的问题/答案。
    • 感谢您的详细说明。这对我有很大帮助。不过,我不还是只按 fi 的值排序吗...(我正在输入此评论,这是我有一点 facepalm 时刻的地方,然后跑到 Visual工作室。)这是解决问题的绝妙方法。而不是将一个值与另一个值与一个巨大的 if/else 进行比较。我只是比较一串值。 string1 = "Singapore Berkeley" string2 = "Singapore Baltimore" 将两个字符串作为一个整体进行比较并从那里排序。杰出的!多谢。非常感谢。
    【解决方案2】:

    您需要传递一个函数对象或一个指向比较两个节点的函数的指针。

    这将允许一个链表函数可以通过传递一个比较函数对象或指向一个比较函数的指针来排序。

    例如,请参阅std::sort

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      相关资源
      最近更新 更多