merge sort/linked lists

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

merge sort/linked lists

 
0
  #1
Apr 2nd, 2009
Hello,
I need to make this program sort the numbers entered with merge sort then print them. I am having trouble calling merge sort and getting it to print.

Here is the code:
  1. #include <iostream>
  2. #include "queueLinkedList.cpp"
  3.  
  4. using namespace std;
  5.  
  6. /*struct Item : Data
  7. {
  8.   int value;
  9.   Item *next;
  10. };*/
  11.  
  12. class mergeSort : public Queue
  13. {
  14. public:
  15. Data* divide(Data*);
  16. Data* merge(Data*, Data*);
  17. Data* mergesort(Data*);
  18. Data* mergesort(void);
  19. Queue queue;
  20. };
  21.  
  22. Data *
  23. mergeSort::divide(Data *a)
  24. {
  25. Data *b, *c;
  26.  
  27. b = a;
  28. c = a->next;
  29. c = c->next;
  30.  
  31. while(c != NULL)
  32. {
  33. c = c->next;
  34. b = b->next;
  35.  
  36. if(c != NULL)
  37. c = c->next;
  38. }
  39.  
  40. c = b->next;
  41. b->next = NULL;
  42.  
  43. return c;
  44. }
  45.  
  46. Data *
  47. mergeSort::merge(Data *p, Data *q)
  48. {
  49. Data *r, *start;
  50. int num;
  51. if(p->value <= q->value)
  52. {
  53. r = p;
  54. num = p->value;
  55. addToQueue(num);
  56. p = p->next;
  57. print();
  58. }
  59.  
  60. else
  61. {
  62. r = q;
  63. num = q->value;
  64. addToQueue(num);
  65. q = q->next;
  66. }
  67.  
  68. start = r;
  69.  
  70. while(p != NULL && q != NULL)
  71. {
  72. if(p->value <= q->value)
  73. {
  74. r->next = p;
  75. r = p;
  76. p = p->next;
  77. }
  78.  
  79. else
  80. {
  81. r->next = q;
  82. r = q;
  83. q = q->next;
  84. }
  85. }
  86.  
  87. if(p == NULL)
  88. r->next = q;
  89. else
  90. r->next = p;
  91.  
  92. return start;
  93. }
  94.  
  95. Data *
  96. mergeSort::mergesort(Data *p)
  97. {
  98. Data *q;
  99. if(p != NULL)
  100. {
  101. if(p->next != NULL)
  102. {
  103. q = divide(p);
  104. p = mergesort(p);
  105. q = mergesort(q);
  106. p = merge(p, q);
  107. }
  108. }
  109. return p;
  110. }
  111.  
  112. Data *
  113. mergeSort::mergesort()
  114. {
  115. mergeSort(top);
  116. }
  117.  
  118. int
  119. main()
  120. {
  121.  
  122. int number = -1;
  123. Data *list;
  124. mergeSort sort;
  125.  
  126. while(number != 0)
  127. {
  128. cout << "Enter a number: " << endl;
  129. cin >> number;
  130. if(number != 0)
  131. {
  132. sort.addToQueue(number);
  133. }
  134. }
  135.  
  136. cout << "Results: " << endl;
  137. sort.print();
  138.  
  139. sort.mergesort();
  140.  
  141. cout << "Sorted results: " << endl;
  142. sort.print();
  143.  
  144. system("pause");
  145. }

  1. /**********************************************
  2. * A program that stores data to a linked list *
  3. * Tests if linked list is full or empty *
  4. * Adds or removes data from linked list *
  5. * Author: Kimberlie Davis *
  6. * Version: 3/26/09 *
  7. ***********************************************/
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. struct Data
  13. {
  14. int value;
  15. Data *next;
  16. };
  17. class Queue
  18. {
  19. private:
  20. int fill, remove;
  21. Data *rear;
  22.  
  23. public:
  24. Queue(void);
  25. void initialize(void);
  26. int takeAway(void);
  27. bool full(void);
  28. bool empty(void);
  29. void addToQueue(int);
  30. Data *top;
  31. int print(void);
  32. //Data * returnTop(void);
  33. };
  34.  
  35. /*
  36. * Constructor
  37. * Initializes fill, remove and count
  38. */
  39. Queue::Queue(void)
  40. {
  41. top = NULL;
  42. rear = NULL;
  43.  
  44. }
  45.  
  46. /*
  47. * method initialize
  48. * Reinitializes the variables
  49. */
  50. void
  51. Queue::initialize()
  52. {
  53. }
  54. /*
  55. * Method takeAway
  56. * Removes items from the list
  57. */
  58. int
  59. Queue::takeAway()
  60. {
  61. Data *p;
  62. char letter;
  63. initialize();
  64. if(top == NULL)
  65. cout << "Queue Overflow" << endl;
  66. else
  67. {
  68. p = top;
  69. letter = p->value;
  70. top = p->next;
  71. delete p;
  72. if(top == NULL)
  73. rear = NULL;
  74. return letter;
  75.  
  76. }
  77. }
  78.  
  79. /*
  80. * method full
  81. * Returns true if list is full
  82. */
  83. bool
  84. Queue::full()
  85. {
  86. if(rear == NULL)
  87. {
  88. cout << "Queue Overflow" << endl;
  89. return true;
  90. }
  91. else
  92. return false;
  93. }
  94.  
  95. /*
  96. * method empty
  97. * returns true if list is empty
  98. */
  99. bool
  100. Queue::empty()
  101. {
  102. if(top == NULL)
  103. {
  104. cout << "Queue Empty" << endl;
  105. return true;
  106. }
  107. else
  108. return false;
  109. }
  110.  
  111. /*
  112. * method addToQueue
  113. * Takes in value from user
  114. * Adds the value to the list
  115. */
  116. void
  117. Queue::addToQueue(int number)
  118. {
  119. Data *p;
  120. p = new Data;
  121. p->value = number;
  122.  
  123. if(top == NULL)
  124. {
  125. top = p;
  126. rear = p;
  127. }
  128.  
  129. else
  130. {
  131. rear->next = p;
  132. rear = p;
  133. }
  134.  
  135. p->next = NULL;
  136.  
  137. }
  138.  
  139. /*Data *
  140. Queue::returnTop()
  141. {
  142.   return top;
  143. }*/
  144. int
  145. Queue::print()
  146. {
  147. Data *p;
  148. p = new Data;
  149. p = top;
  150. // Data *p;
  151. //p = new Data;
  152. int num;
  153.  
  154.  
  155. while(top != NULL)
  156. {
  157. num = p->value;
  158. top = p->next;
  159. p = p->next;
  160.  
  161. cout << num << endl;
  162. }
  163. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: merge sort/linked lists

 
0
  #2
Apr 2nd, 2009
I got it partly figured out, top was being set to NULL in the print() method. So I made it so top won't be NULL anymore.

But I'm still having a problem, I think because when I addToQueue in merge its adding to what is already there.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
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