[英]bubble sort for linked list in c++
我試圖為我的大學項目實施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;
}
鏈接列表應按日期和小時排序。 但是我有一些編譯問題。 我完全需要您的幫助,謝謝:)
總的來說,我會解決以下問題:
您的類TrainManager的char *成員不是std :: string,並且您沒有管理內存。 更不用說所有成員都是私人的,當您嘗試比較其成員時,這可能會給您帶來問題。
您最好交換“鏈接”,而不是交換其中的實際數據。
編譯器錯誤非常明顯。 使用Swap(*(curr->m_data),*(curr->m_next->m_data));
而不是Swap(&(curr->m_data),&(curr->m_next->m_data));
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.