Sorting/Returning a Linked List

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 11
Reputation: teddybouch is an unknown quantity at this point 
Solved Threads: 0
teddybouch teddybouch is offline Offline
Newbie Poster

Sorting/Returning a Linked List

 
0
  #1
Jan 26th, 2009
I am trying to sort a linked list, and not being terribly familiar with sorting algorithms, I just found a merge sort on Wikipedia and decided to go with it. Hence, I apologize if I'm missing something obvious. Based on the printout statements that I inserted into the code, it looks like my problem has to do with returning the linked list - everything proceeds normally until I need to make the first return, and then it just crashes.

Here's the sorting code. It is completely based on the example pseudocode shown as an example in the Wikipedia article, with changes made to syntax to fit my application.

  1. static list mergeSortX( list sortMe )
  2. {
  3. // Declare variables
  4. list left, right, result;
  5. int middle;
  6. double contents[2];
  7. double temp[2];
  8.  
  9. printf("In mergeSortX with length %d\n", sortMe.count());
  10.  
  11. // Return if the length is one or less
  12. if ( sortMe.count() <= 1 )
  13. {
  14. printf("returning = ");
  15. for ( int i = 0; i < sortMe.count(); i++ )
  16. {
  17. sortMe.access( i, temp );
  18. printf( "(%f, %f), ", temp[0], temp[1] );
  19. }
  20. printf("\n");
  21.  
  22. printf("Sorted by length, returning...\n");
  23. return sortMe;
  24. }
  25.  
  26. // Find the middle index of the list
  27. middle = sortMe.count() / 2;
  28. printf("middle = %d\n", middle);
  29.  
  30. // Take elements 0 to middle and put them in left
  31. for( int i = 0; i < middle; i++ )
  32. {
  33. sortMe.access( i, contents );
  34. left.append( contents[0], contents[1] );
  35. }
  36.  
  37. printf("left = ");
  38. for ( int i = 0; i < left.count(); i++ )
  39. {
  40. left.access( i, temp );
  41. printf( "(%f, %f), ", temp[0], temp[1] );
  42. }
  43. printf("\n");
  44.  
  45.  
  46. // Take elements middle to end and put them in right
  47. for( int j = middle; j < sortMe.count(); j++ )
  48. {
  49. sortMe.access( j, contents );
  50. right.append( contents[0], contents[1] );
  51. }
  52.  
  53. printf("right = ");
  54. for ( int i = 0; i < right.count(); i++ )
  55. {
  56. right.access( i, temp );
  57. printf( "(%f, %f), ", temp[0], temp[1] );
  58. }
  59. printf("\n");
  60.  
  61.  
  62. // Sort the left and right lists recursively
  63. left = mergeSortX( left );
  64. printf("Sorted left = ");
  65. for ( int i = 0; i < left.count(); i++ )
  66. {
  67. left.access( i, temp );
  68. printf( "(%f, %f), ", temp[0], temp[1] );
  69. }
  70. printf("\n");
  71. right = mergeSortX( right );
  72.  
  73. // Merge the result into one list
  74. printf("Merging lists...\n");
  75. result = mergeX( left, right );
  76.  
  77. printf("result = ");
  78. for ( int i = 0; i < result.count(); i++ )
  79. {
  80. result.access( i, temp );
  81. printf( "(%f, %f), ", temp[0], temp[1] );
  82. }
  83. printf("\n");
  84.  
  85. printf("Leaving mergeSortX\n");
  86.  
  87. return result;
  88. }
  89.  
  90. static list mergeX( list left, list right )
  91. {
  92. // Declare variables
  93. list result;
  94. double firstLeft[2];
  95. double firstRight[2];
  96.  
  97. // Merge the lists, maintaining order
  98. while ( ( left.count() > 0 ) and ( right.count() > 0 ) )
  99. {
  100. left.access( 0, firstLeft );
  101. right.access( 0, firstRight );
  102. if ( firstLeft[0] <= firstRight[0] )
  103. {
  104. // Append the first element of left to the result and remove from left
  105. result.append( firstLeft[0], firstLeft[1] );
  106. left.del( firstLeft[0], firstLeft[1] );
  107. }
  108. else
  109. {
  110. // Append the first element of right to the result and remove from right
  111. result.append( firstLeft[0], firstLeft[1] );
  112. right.del( firstLeft[0], firstLeft[1] );
  113. }
  114. }
  115.  
  116. while ( left.count() > 0 )
  117. {
  118. left.access( 0, firstLeft );
  119. result.append( firstRight[0], firstRight[1] );
  120. left.del( firstRight[0], firstRight[1] );
  121. }
  122.  
  123. while ( right.count() > 0 )
  124. {
  125. right.access( 0, firstRight );
  126. result.append( firstRight[0], firstRight[1] );
  127. right.del( firstRight[0], firstRight[1] );
  128. }
  129.  
  130. return result;
  131.  
  132. }

The next thing I wanted to include was my linked list class, in case the problem lies there.

  1. class list
  2. {
  3. private:
  4.  
  5. struct node
  6. {
  7. double xData;
  8. double yData;
  9. node *link;
  10. }*p;
  11.  
  12. public:
  13.  
  14. list();
  15. void append( double x, double y );
  16. void del( double x, double y );
  17. void access( int index, double contents[] );
  18. int count();
  19. ~list();
  20. };
  21.  
  22. list::list()
  23. {
  24. p=NULL;
  25. }
  26.  
  27. void list::append( double x, double y )
  28. {
  29. node *q,*t;
  30.  
  31. if( p == NULL )
  32. {
  33. p = new node;
  34. p->xData = x;
  35. p->yData = y;
  36. p->link = NULL;
  37. }
  38. else
  39. {
  40. q = p;
  41. while( q->link != NULL )
  42. {
  43. q = q->link;
  44. }
  45.  
  46. t = new node;
  47. t->xData = x;
  48. t->yData = y;
  49. t->link = NULL;
  50. q->link = t;
  51. }
  52. }
  53.  
  54. void list::del( double x, double y )
  55. {
  56. node *q,*r;
  57. q = p;
  58.  
  59. if(( q->xData == x ) && ( q->yData == y ))
  60. {
  61. p = q->link;
  62. delete q;
  63. return;
  64. }
  65.  
  66. r = q;
  67. while( q!=NULL )
  68. {
  69. if(( q->xData == x ) && ( q->yData == y ))
  70. {
  71. r->link = q->link;
  72. delete q;
  73. return;
  74. }
  75.  
  76. r = q;
  77. q = q->link;
  78. }
  79. printf("Element (%f, %f) not Found.\n", x, y);
  80. }
  81.  
  82. void list::access( int index, double contents[])
  83. {
  84. node *q;
  85. int c=0;
  86. for( q=p ; c<index ; c++ )
  87. {
  88. q = q->link;
  89. }
  90.  
  91. contents[0] = q->xData;
  92. contents[1] = q->yData;
  93. }
  94.  
  95. int list::count()
  96. {
  97. node *q;
  98. int c=0;
  99. for( q=p ; q != NULL ; q = q->link )
  100. c++;
  101.  
  102. return c;
  103. }
  104.  
  105. list::~list()
  106. {
  107. node *q;
  108. if( p == NULL )
  109. return;
  110.  
  111. while( p != NULL )
  112. {
  113. q = p->link;
  114. delete p;
  115. p = q;
  116. }
  117. }

Finally, here's some test output that I have in case it will shed some light on what is going on:

  1. doX = 1
  2. List before = (-5.617623, -2.034482), (-5.506881, -2.153052), (-5.377584, -2.276156), (-5.249851, -2.403839), (-5.120810, -2.536295), (-4.969870, -2.675439), (-4.833482, -2.818779), (-5.732594, -1.919852), (-5.851091, -1.809288), (-5.956030, -1.701978), (-6.108007, -1.492484),
  3. In mergeSortX with length 11
  4. middle = 5
  5. left = (-5.617623, -2.034482), (-5.506881, -2.153052), (-5.377584, -2.276156), (-5.249851, -2.403839), (-5.120810, -2.536295),
  6. right = (-4.969870, -2.675439), (-4.833482, -2.818779), (-5.732594, -1.919852), (-5.851091, -1.809288), (-5.956030, -1.701978), (-6.108007, -1.492484),
  7. In mergeSortX with length 5
  8. middle = 2
  9. left = (-5.617623, -2.034482), (-5.506881, -2.153052),
  10. right = (-5.377584, -2.276156), (-5.249851, -2.403839), (-5.120810, -2.536295),
  11. In mergeSortX with length 2
  12. middle = 1
  13. left = (-5.617623, -2.034482),
  14. right = (-5.506881, -2.153052),
  15. In mergeSortX with length 1
  16. returning = (-5.617623, -2.034482),
  17. Sorted by length, returning...

Thanks very much for the help!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC