开发者

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:

  1. 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.

  2. 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));.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜