another merge sort question

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

Join Date: Nov 2004
Posts: 20
Reputation: skeet123 is an unknown quantity at this point 
Solved Threads: 0
skeet123 skeet123 is offline Offline
Newbie Poster

another merge sort question

 
0
  #1
Dec 2nd, 2004
I have been working on this program awhile and I am almost done with it except for trying to get the output to look like what instructor wants. Here how the output should look:

[PHP]BREAKING DOWN: list = DUMP: (size = 4, first = 4, last = 3)
DUMP: head = : 4
DUMP: data = : 2
DUMP: data = : 5
DUMP: data = : 3

BREAKING DOWN: list = DUMP: (size = 2, first = 4, last = 2)
DUMP: head = : 4
DUMP: data = : 2

BREAKING DOWN: list = DUMP: (size = 1, first = 4, last = 4)
DUMP: head = : 4

BREAKING DOWN: list = DUMP: (size = 1, first = 2, last = 2)
DUMP: head = : 2

MERGED: list = DUMP: (size = 2, first = 2, last = 4)
DUMP: head = : 2
DUMP: data = : 4

BREAKING DOWN: list = DUMP: (size = 2, first = 5, last = 3)
DUMP: head = : 5
DUMP: data = : 3

BREAKING DOWN: list = DUMP: (size = 1, first = 5, last = 5)
DUMP: head = : 5

BREAKING DOWN: list = DUMP: (size = 1, first = 3, last = 3)
DUMP: head = : 3

MERGED: list = DUMP: (size = 2, first = 3, last = 5)
DUMP: head = : 3
DUMP: data = : 5

MERGED: list = DUMP: (size = 4, first = 2, last = 5)
DUMP: head = : 2
DUMP: data = : 3
DUMP: data = : 4
DUMP: data = : 5 [/PHP]

Here is my code it is pretty long. I am using a linked list to store the nodes. I have tried putting cout statements and using my dump() from my linked list in different parts of the code but I cant get the output right any help would be
much appreciated. CODE:

  1. class node
  2. {
  3. public:
  4. typedef int data_t;
  5.  
  6. node(data_t d) { next = NULL; data = d; }
  7.  
  8. node *next;
  9. data_t data;
  10. };
  11.  
  12. class linked_list
  13. {
  14. private:
  15. node *head;
  16.  
  17. public:
  18. linked_list()
  19. {
  20. head = NULL;
  21. }
  22.  
  23. int size()
  24. {
  25. int length = 0;
  26. node *sizeptr = head;
  27. while(sizeptr != NULL)
  28. {
  29. sizeptr = sizeptr->next;
  30. length++;
  31. }
  32. return length;
  33. }
  34.  
  35. void add(int n, node::data_t d)
  36. {
  37. int number=0;
  38. node *tmp;
  39. node *nhp = new node(d);
  40. if(head == NULL){
  41. head=nhp;
  42. }else if(n == 0){
  43. nhp->next = head;
  44. head = nhp;
  45. }else{
  46. tmp=head;
  47. while((tmp->next != NULL) & (number < n-1)){
  48. tmp = tmp->next;
  49. number++;
  50. }
  51. nhp->next=tmp->next;
  52. tmp->next=nhp;
  53. }
  54. }
  55.  
  56. void add_last(node::data_t d)
  57. {
  58. add(size(),d);
  59. }
  60.  
  61. void add_first(node::data_t d)
  62. {
  63. add(0,d);
  64. }
  65.  
  66. node::data_t get(int n)
  67. {
  68. int number=0;
  69. node *gptr = head;
  70. if(head == NULL){
  71. cout << "List is empty!" <<endl;
  72. return 0;
  73. }else if(n == 0){
  74. return gptr->data;
  75. }else if(n == size()-1){
  76. while(gptr->next != NULL)
  77. {
  78. gptr=gptr->next;
  79. }
  80. return gptr->data;
  81.  
  82. }else if(n >= size()-1){
  83. cout << "non existent index" << endl;
  84. return 0;
  85. }else if((n > 0) & (n <= size()-1)){
  86. while(number < n)
  87. {
  88. gptr = gptr->next;
  89. number++;
  90. }
  91. return gptr->data;
  92. }
  93.  
  94. return 0;
  95. }
  96.  
  97. node::data_t get_first()
  98. {
  99. return get(0);
  100. }
  101.  
  102. node::data_t get_last()
  103. {
  104. return get(size()-1);
  105. }
  106.  
  107. void remove(int n)
  108. {
  109. int number=0;
  110. node *tmp, *current;
  111. if(head == NULL){
  112. cout << "The list is empty!" << endl;
  113. }else{
  114. current=head;
  115. if(current->next==NULL){
  116. delete(current);
  117. head=NULL;
  118. }else if(n == 0){
  119. current=head;
  120. head=head->next;
  121. delete(current);
  122. }else if(n > size()-1){
  123. cout << "nonexistent indez" << endl;
  124. }else{
  125. while((current->next != NULL) & (number < n))
  126. {
  127. tmp = current;
  128. current = current->next;
  129. number++;
  130. }
  131. tmp->next=current->next;
  132. delete(current);
  133. }
  134. }
  135. }
  136.  
  137. void remove_first()
  138. {
  139. remove(0);
  140. }
  141.  
  142. void remove_last()
  143. {
  144. remove(size()-1);
  145. }
  146.  
  147. void dump()
  148. {
  149. node *tptr;
  150.  
  151. cout << " DUMP: (size = " << size() << ", first = " << get_first()
  152. <<", last = " << get_last() << ")\n" ;
  153. if (head == NULL) {
  154. cout << " DUMP: head = NULL\n\n";
  155. return;
  156. }
  157. tptr = head;
  158.  
  159. cout << " DUMP: head = : " << tptr->data << endl;
  160. while (tptr->next != NULL) {
  161. tptr = tptr->next;
  162. cout << " DUMP: data = : " << tptr->data << endl;
  163. }
  164. cout << endl;
  165. }
  166. };
  167. class mergesort
  168. {
  169.  
  170. public:
  171.  
  172. mergesort(int ar[], int len);
  173.  
  174. void mergesortlink(linked_list& list);
  175.  
  176. void merge(linked_list& list1, linked_list& list2, linked_list& list);
  177. };
  178.  
  179.  
  180.  
  181.  
  182. mergesort::mergesort(int ar[], int len)
  183. {
  184. int i;
  185.  
  186. linked_list alist;
  187.  
  188.  
  189. for (i=0; i<len; i++){
  190. alist.add_last(ar[i]);
  191. }
  192. alist.dump();
  193.  
  194. mergesortlink(alist);
  195.  
  196. for(i=0;i<len;i++)
  197. {
  198. ar[i] = alist.get_first();
  199. alist.remove_first();
  200. }
  201. }
  202.  
  203.  
  204. void mergesort::mergesortlink(linked_list& list)
  205. {
  206. int i;
  207. int d;
  208. linked_list list1;
  209. linked_list list2;
  210.  
  211. int n = list.size();
  212. if(n < 2){
  213. return;
  214. }
  215. for (i=1; i <= (n+1)/2; i++)
  216. {
  217. d = list.get_first();
  218. list.remove(0);
  219. list1.add_last(d);
  220. }
  221.  
  222. for (i=1; i <= n/2; i++)
  223. {
  224. d = list.get_first();
  225. list.remove(0);
  226. list2.add_last(d);
  227. }
  228.  
  229. mergesortlink(list1);
  230. mergesortlink(list2);
  231.  
  232. merge(list1,list2,list);
  233.  
  234. }
  235.  
  236.  
  237. void mergesort::merge(linked_list& list1, linked_list& list2, linked_list& list)
  238. {
  239. int d,d1,d2;
  240.  
  241. while(list1.size()!=0 && list2.size()!=0)
  242. {
  243.  
  244. d1 = list1.get_first();
  245. d2 = list2.get_first();
  246.  
  247. if(d1 < d2)
  248. {
  249. d = list1.get_first();
  250. list1.remove(0);
  251. list.add_last(d);
  252. list.dump();
  253. }else{
  254. d = list2.get_first();
  255. list2.remove(0);
  256. list.add_last(d);
  257. list.dump();
  258. }
  259. }
  260.  
  261. if(list1.size()==0)
  262. {
  263.  
  264. while(list2.size()!=0)
  265. {
  266. d = list2.get_first();
  267. list2.remove(0);
  268. list.add_last(d);
  269. list.dump();
  270. }
  271. }
  272.  
  273. if(list2.size()==0)
  274. {
  275. while(list1.size()!=0)
  276. {
  277. d = list1.get_first();
  278. list1.remove(0);
  279. list.add_last(d);
  280. list.dump();
  281. }
  282.  
  283. }
  284.  
  285. }
  286. int main(void)
  287. {
  288.  
  289. int ar[100];
  290.  
  291. int i, v, len;
  292.  
  293. for (i=0; i<100; i++) {
  294. cout << "enter a number (-1 to quit): ";
  295. cin >> v;
  296.  
  297. if (v < 0) break;
  298.  
  299. ar[i] = v;
  300. }
  301.  
  302. len = i;
  303.  
  304. cout << "main: before sort:\n";
  305. for (i=0; i<len; i++) {
  306. cout << "main: ar[" << i << "] = " << ar[i] << endl;
  307. }
  308.  
  309. mergesort ms(ar, len);
  310. cout << "main: after sort:\n";
  311. for (i=0; i<len; i++) {
  312. cout << "main: ar[" << i << "] = " << ar[i] << endl;
  313. }
  314.  
  315. // return 0;
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: another merge sort question

 
0
  #2
Dec 23rd, 2004
what exact problem do you have? is it the merging and sorting? try traversing the second list, adding each element to the first. This will merge two lists. To sort use any method you like, but it may be easy to convert the complete list to an array OR overload the [] operator to work like an array subscript operator. I have posted a c/c++ snippet about linked list if it helps
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC