943,536 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 576
  • C++ RSS
Apr 1st, 2009
0

Linked Lists problem

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Apr 2nd, 2009
1

Re: Linked Lists problem

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.
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009
Apr 2nd, 2009
0

Re: Linked Lists problem

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.
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Apr 2nd, 2009
0

Re: Linked Lists problem

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.
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Apr 2nd, 2009
0

Re: Linked Lists problem

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."
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009
Apr 2nd, 2009
0

Re: Linked Lists problem

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!
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: program won't compile. Please help.
Next Thread in C++ Forum Timeline: merge sort/linked lists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC