943,907 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 697
  • C++ RSS
Aug 5th, 2008
0

Circular List help

Expand Post »
The circular list code is incomplete program with lots of feature missing, it's only one specific section that I require help with but I thought it would better to show the entirety of it to help you understand better.

The section surrounded by -------------------------- is where I'm stuck, basically the print() method should start at the first element and go through the circular list printing each element until it returns to the first element. The program then rotates the list and prints each element again.

I feel that it maybe the "do...while" loop is causing the program since when I changed the "while" expression to "(c = while)", obviously wrong as it never ended, it successfully ran through the program and printed each element but when it's "(c != while)" it fails.

If anyone can offer any assistance as to what I've done wrong then it would be greatly appreciated, I've racked my brains for long time now! (I suck)

c++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. struct listitem {
  6. int contents;
  7. struct listitem* next;
  8. struct listitem* prev;
  9. };
  10.  
  11. class rotatableList {
  12. protected:
  13. listitem *first;
  14. int size;
  15. public:
  16. rotatableList() {
  17. first=NULL;
  18. size=0;
  19. }
  20. };
  21.  
  22. class singleRotateList : public rotatableList {
  23. public:
  24. void addFirst(int i) {
  25. // Adds a new list element with contents i
  26. // at the start of the list
  27.  
  28. struct listitem* newItem=new listitem();
  29.  
  30. newItem->contents=i;
  31.  
  32. if (!first) {
  33. newItem->next=newItem;
  34. newItem->prev=newItem;
  35. first=newItem;
  36. }
  37. else {
  38. newItem->next=first;
  39. newItem->prev=first->prev;
  40. first->prev->next=newItem;
  41. first->prev=newItem;
  42. first=newItem;
  43.  
  44. }
  45.  
  46. // Omitted: code to increment "size"
  47. size++;
  48. }
  49. //---------------------------------------------------------------------
  50. void print() {
  51. listitem *c=first;
  52.  
  53. if (c) {
  54. printf("%d\n", *c);
  55. listitem *c=first=first->next;
  56.  
  57. do{
  58. printf("%d\n", *c);
  59. listitem *c=first=first->next;
  60. }while (c != first);
  61. }
  62. }
  63. // Omitted: code to
  64. // - print out contents of list element referred to by c
  65. // (followed by a new line);
  66. // - make c point to the next element in the list;
  67. // - Do the following while c doesn't refer to the same list
  68. // element referred to by first:
  69. // - print out contents of list element referred to by c
  70. // (followed by a new line);
  71. // - make c point to the next element in the list;
  72. //----------------------------------------------------------------------
  73.  
  74. void rotate() {
  75. // Make first refer to the next item in the list
  76. if (first) first=first->next;
  77. }
  78. };
  79.  
  80. class randomRotateList : public rotatableList {
  81. public:
  82. void rotate() {
  83. if (first) {
  84. }
  85. }
  86. };
  87.  
  88. main() {
  89. srand(time(NULL)); // This initialises the random number generator
  90.  
  91. printf("Creating a singleRotateList and rotating it:\n");
  92. singleRotateList* l=new singleRotateList();
  93. l->addFirst(3);
  94. l->addFirst(4);
  95. l->addFirst(5);
  96. l->addFirst(6);
  97. l->print();
  98.  
  99. l->rotate();
  100. printf("\nAfter rotation:\n");
  101. l->print();
  102.  
  103. delete l;
  104.  
  105. printf("\nNow using a randomRotateList:\n");
  106. }
Last edited by Corum; Aug 5th, 2008 at 4:12 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Corum is offline Offline
6 posts
since Nov 2006
Aug 5th, 2008
0

Re: Circular List help

Well you probably shouldn't be modifying first, just to print it.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 5th, 2008
0

Re: Circular List help

You have three cs there:
line 51
line 55
line 59
Your loop will never terminate because every c is always == first (so no matter which of the three the compiler chooses to use you've got an infinite loop).

Make sure to use just one c.

Also, watch how many times you change first. You want to bump it just once (if I understand you right).
C++ Syntax (Toggle Plain Text)
  1. void print() {
  2. listitem *c=first;
  3.  
  4. if (c) {
  5. printf("%d\n", *c);
  6. c=first=first->next;
  7.  
  8. do{
  9. printf("%d\n", *c);
  10. c=c->next;
  11. }while (c != first);
  12. }
  13. }
Hope this helps.

[edit] BTW. What if your circular list has just one item? (It will get printed twice.)
Last edited by Duoas; Aug 5th, 2008 at 4:28 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Aug 5th, 2008
0

Re: Circular List help

Thanks a lot for helping me understand where I was going wrong and explaining it so well you've been a huge help.

The circular list always have 4 items (in main() you see them added to addFirst()) so it shouldn't be a problem.

Thank you again!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Corum is offline Offline
6 posts
since Nov 2006
Aug 6th, 2008
0

Re: Circular List help

Last problem I promise! I've succesfully got the singleRotateList() to work and output but after that's executed I want it execute the randomRotateList() and output accordingly.

The problem is the main() method I have calling it two different rotate() methods but it keeps referring to the first rotate(), ideally I want it to call to the first for the singleRotateList() and the second rotate() for randomRotateList().

I'm not to hot on abstract and overridable methods so if anyone can give me tips as to how I can achieve this it would be much appreciated, again!

c++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. struct listitem {
  6. int contents;
  7. struct listitem* next;
  8. struct listitem* prev;
  9. };
  10.  
  11. class rotatableList {
  12.  
  13. protected:
  14. listitem *first;
  15. int size;
  16. public:
  17. rotatableList() {
  18. first=NULL;
  19. size=0;
  20. }
  21.  
  22. void addFirst(int i) {
  23.  
  24. struct listitem* newItem=new listitem();
  25.  
  26. newItem->contents=i;
  27.  
  28. if (!first) {
  29. newItem->next=newItem;
  30. newItem->prev=newItem;
  31. first=newItem;
  32. }
  33. else {
  34. newItem->next=first;
  35. newItem->prev=first->prev;
  36. first->prev->next=newItem;
  37. first->prev=newItem;
  38. first=newItem;
  39. }
  40.  
  41. size++;
  42.  
  43. }
  44.  
  45. void print() {
  46. listitem *c=first;
  47.  
  48. if (c) {
  49. printf("%d\n", *c);
  50. c=first->next;
  51. do{
  52. printf("%d\n", *c);
  53. c=c->next;
  54. }while (c != first);
  55. }
  56. }
  57.  
  58. void rotate() {
  59.  
  60. if (first) first=first->next;
  61.  
  62. }
  63.  
  64. };
  65.  
  66. class singleRotateList : public rotatableList {
  67. public:
  68.  
  69.  
  70. };
  71.  
  72. class randomRotateList : public rotatableList {
  73. public:
  74. void rotate() {
  75. if (first) {
  76.  
  77. int numberOfTimes = rand() % 4;
  78. printf("The list will be rotated %d times.\n", numberOfTimes);
  79. for (int i = 0; i<numberOfTimes; i++){
  80. first=first->next;
  81. }
  82. }
  83. }
  84. };
  85. ---------------------------------------------------------------------------------
  86. main() {
  87. srand(time(NULL)); // This initialises the random number generator
  88.  
  89. printf("Creating a singleRotateList and rotating it:\n");
  90. rotatableList* l=new singleRotateList();
  91. l->addFirst(3);
  92. l->addFirst(4);
  93. l->addFirst(5);
  94. l->addFirst(6);
  95. l->print();
  96.  
  97. l->rotate();
  98. printf("\nAfter rotation:\n");
  99. l->print();
  100.  
  101. delete l;
  102.  
  103. printf("\nNow using a randomRotateList:\n");
  104.  
  105. l=new randomRotateList();
  106. l->addFirst(3);
  107. l->addFirst(4);
  108. l->addFirst(5);
  109. l->addFirst(6);
  110. l->print();
  111.  
  112. l->rotate();
  113. printf("\nAfter rotation:\n");
  114. l->print();
  115. ---------------------------------------------------------------------------------
  116. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Corum is offline Offline
6 posts
since Nov 2006
Aug 6th, 2008
0

Re: Circular List help

Use magical word virtual in
C++ Syntax (Toggle Plain Text)
  1. class rotatableList {
  2. ...
  3. virtual void rotate () ...
  4. ...
  5. };
and try again...
Last edited by ArkM; Aug 6th, 2008 at 12:32 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 6th, 2008
0

Re: Circular List help

That indeed is a magical word, thanks for the help! Hugely appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Corum is offline Offline
6 posts
since Nov 2006

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: trouble with multi text char
Next Thread in C++ Forum Timeline: Linking error in the compiler given by topcoder. Plz help me out guys.





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


Follow us on Twitter


© 2011 DaniWeb® LLC