bubble sort for linked list in c++
i tried to implement buble sort for my university project and i have some problems. i will be happy if you can help me with it.
void TrainManagerLinkedList:: Swap(TrainManager & x,TrainManager & y)
{
TrainManager temp;
temp =x;开发者_JAVA技巧
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 :)*/
}
my data types
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;
}
the linked list should be sorted by date and hour. but i have some compile problems. i relly need your help thank you,:)
In general there are issues I would address as follows:
Your class TrainManager has char* members not std::string, and you are not managing the memory. Not to mention that all the members are private, which may give you issues when you try comparing its members.
You would do better to swap the "links" rather than swap the actual data inside them.
The compiler error is quite obvious.
Use Swap(*(curr->m_data),*(curr->m_next->m_data));
instead of Swap(&(curr->m_data),&(curr->m_next->m_data));
.
精彩评论