2010-12-21 83 views
0

我試圖爲我的大學項目實現佈雷排序,並且我有一些問題。 如果你能幫助我,我會很高興。C++中的鏈表的氣泡排序

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

你能將所有代碼格式化爲代碼嗎?謝謝! – 2010-12-21 09:32:28

+2

什麼是編譯器錯誤的確切用詞? – 2010-12-21 09:34:02

+1

此問題缺少相當多的上下文信息。請閱讀[「關於您的問題的準確性和內容豐富性」](http://www.catb.org/~esr/faqs/smart-questions.html#beprecise),[「詢問代碼時」](http:/ /www.catb.org/~esr/faqs/smart-questions.html#code)和[「寫出完美的問題」](http://tinyurl.com/so-hints)。 – outis 2010-12-21 09:42:53

回答

1

編譯器錯誤是相當明顯的。 使用Swap(*(curr->m_data),*(curr->m_next->m_data)); 而不是Swap(&(curr->m_data),&(curr->m_next->m_data));

2

一般有我能解決的問題如下:

  1. 你的類TrainManager擁有的char *成員不要的std :: string,和你沒有管理的內存。更不用說所有的成員都是私人的,當你試圖比較它的成員時,這可能會給你帶來問題。

  2. 你會更好地交換「鏈接」,而不是交換其中的實際數據。