Linked Lists problem

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

Linked Lists problem

 
0
  #1
Apr 1st, 2009
Hello,
I am having trouble understanding linked lists. I have to make this program that takes input, stores it to a queue, outputs that input, then sorts it using mergesort and outputs it again. It all works except I'm not sure how to call the mergesort classes.

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

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 33
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: Linked Lists problem

 
1
  #2
Apr 2nd, 2009
You seem to have two classes, Queue and mergeSort. mergeSort is derived from Queue. mergeSort is using a different struct for it's nodes than Queue, which isn't helping. I suggest you use struct Data for the nodes in both classes.
As mergeSort is a Queue, in main() you'll be wanting to use mergeSort as your Queue instead of Queue. So replace "Queue sort;" by "mergeSort sort;".
Now, you should be able to do everything as you are already, but add "sort.mergesort();" to main() where you want to do the sorting.
Of course you'll have to uncomment the only line in mergesort().
Other problems:
The mergeSort methods aren't members of the class. E.g divide() should be mergeSort::divide().
Your naming is poor. mergesort is a verb and should not generally be used to name a class. If you have a queue which provides sorting, it could be a SortableQueue, but not a mergesort.
In Main() you call the Queue "sort". It isn't a "sort", it's a queue, so call it "queue". Just use the verbs for naming methods. I realise, that at this stage you just want to get something running and you don't see names as important, but it really does help. If you can't name it properly, you should ask yourself if you actually know what it's doing.
Inheritance problem - "class mergeSort:Queue" - make that "class mergeSort : public Queue", always do that when deriving classes, unless you have strong reasons not to.
In Main(), "number" is not initialised. That will cause annoying intermittant problems. Think about it.

Hopefully this gets you a bit further. Note that I haven't considered any details of the sorting or queue management code, so good luck with all that.
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: Linked Lists problem

 
0
  #3
Apr 2nd, 2009
So about the struct, should I completely take the struct out of mergeSort? I tried doing, and changed everything in mergeSort to Data that and it caused errors. Then I tried making Item derive from Data, and changed everything to being Data items ,that caused some errors still.
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: Linked Lists problem

 
0
  #4
Apr 2nd, 2009
Never mind, taking out the struct Item worked.
But now, you said that i need to change Queue sort to mergeSort sort, i did that and then an error was comming up in queue saying that addToQueue can not be accessed.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 33
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: Linked Lists problem

 
0
  #5
Apr 2nd, 2009
Originally Posted by christiangirl View Post
Never mind, taking out the struct Item worked.
But now, you said that i need to change Queue sort to mergeSort sort, i did that and then an error was comming up in queue saying that addToQueue can not be accessed.
Are you sure you did this:-
"Inheritance problem - "class mergeSort:Queue" - make that "class mergeSort : public Queue", always do that when deriving classes, unless you have strong reasons not to."
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: Linked Lists problem

 
0
  #6
Apr 2nd, 2009
oh your right! Sorry, I don't know my problem this week, I keep forgetting stuff like that! I usually remember to do that and put mergeSort:: in front of all of the methods!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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