gpt4 book ai didi

c++ - C++中链表的冒泡排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:48:54 25 4
gpt4 key购买 nike

我尝试为我的大学项目实现 buble 排序,但我遇到了一些问题。如果你能帮助我,我会很高兴。

void TrainManagerLinkedList:: Swap(TrainManager & x,TrainManager & y)
{
TrainManager temp;
temp =x;
x = y;
y = temp;
}

void TrainManagerLinkedList::BubbleSort()
{
TrainManagerLink* outerCurr = this->m_head;
TrainManagerLink* curr = NULL;

while(outerCurr != NULL)
{
curr = this->m_head;
while(curr != NULL && curr->m_next != NULL)
{
/*if the current link greater then the next swap between them*/
if (curr->m_data->GetDate() > curr->m_next->m_data->GetDate())
{
Swap(&(curr->m_data),&(curr->m_next->m_data));
}
else if((curr->m_data->GetDate() == curr->m_next->m_data->GetDate())&(curr->m_data->GetTime() > curr->m_next->m_data->GetTime()))
{
Swap(&(curr->m_data),&(curr->m_next->m_data));
}
curr = curr->m_next;
}
outerCurr = outerCurr->m_next;
}
/*now the list is sorted :)*/

}

我的数据类型

TrainManagerLink *m_head;
TrainManagerLink *m_tail;
int m_numOfElements;

class TrainManager
{
char * m_firstStation;
char *m_lastStation;
char * m_origin;
char * m_destination;
int m_timeBetweenStations;
Hour m_releaseTime;
Hour m_arriveTime;
Hour m_firstHour;
Date m_Date;
int m_standInstation;
DelayersLinkedList delay;
}

链表应该按日期和时间排序。但我有一些编译问题。我真的需要你的帮助谢谢,:)

最佳答案

一般来说,我会解决以下问题:

  1. 您的类 TrainManager 有 char* 成员而不是 std::string,并且您没有管理内存。更不用说所有成员都是私有(private)的,这在您尝试比较其成员时可能会给您带来问题。

  2. 最好交换“链接”而不是交换其中的实际数据。

关于c++ - C++中链表的冒泡排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4497820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com