Circular List help

Thread Solved

Join Date: Nov 2006
Posts: 6
Reputation: Corum is an unknown quantity at this point 
Solved Threads: 0
Corum Corum is offline Offline
Newbie Poster

Circular List help

 
0
  #1
Aug 5th, 2008
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)

  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 3:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,590
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 851
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Circular List help

 
0
  #2
Aug 5th, 2008
Well you probably shouldn't be modifying first, just to print it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,979
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 220
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Circular List help

 
0
  #3
Aug 5th, 2008
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).
  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 3:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: Corum is an unknown quantity at this point 
Solved Threads: 0
Corum Corum is offline Offline
Newbie Poster

Re: Circular List help

 
0
  #4
Aug 5th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: Corum is an unknown quantity at this point 
Solved Threads: 0
Corum Corum is offline Offline
Newbie Poster

Re: Circular List help

 
0
  #5
Aug 6th, 2008
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!

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Circular List help

 
0
  #6
Aug 6th, 2008
Use magical word virtual in
  1. class rotatableList {
  2. ...
  3. virtual void rotate () ...
  4. ...
  5. };
and try again...
Last edited by ArkM; Aug 6th, 2008 at 11:32 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: Corum is an unknown quantity at this point 
Solved Threads: 0
Corum Corum is offline Offline
Newbie Poster

Re: Circular List help

 
0
  #7
Aug 6th, 2008
That indeed is a magical word, thanks for the help! Hugely appreciated.
Reply With Quote Quick reply to this message  
Reply

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




Views: 614 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC