| | |
Circular List help
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
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)
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)
#include <stdio.h> #include <stdlib.h> #include <time.h> struct listitem { int contents; struct listitem* next; struct listitem* prev; }; class rotatableList { protected: listitem *first; int size; public: rotatableList() { first=NULL; size=0; } }; class singleRotateList : public rotatableList { public: void addFirst(int i) { // Adds a new list element with contents i // at the start of the list struct listitem* newItem=new listitem(); newItem->contents=i; if (!first) { newItem->next=newItem; newItem->prev=newItem; first=newItem; } else { newItem->next=first; newItem->prev=first->prev; first->prev->next=newItem; first->prev=newItem; first=newItem; } // Omitted: code to increment "size" size++; } //--------------------------------------------------------------------- void print() { listitem *c=first; if (c) { printf("%d\n", *c); listitem *c=first=first->next; do{ printf("%d\n", *c); listitem *c=first=first->next; }while (c != first); } } // Omitted: code to // - print out contents of list element referred to by c // (followed by a new line); // - make c point to the next element in the list; // - Do the following while c doesn't refer to the same list // element referred to by first: // - print out contents of list element referred to by c // (followed by a new line); // - make c point to the next element in the list; //---------------------------------------------------------------------- void rotate() { // Make first refer to the next item in the list if (first) first=first->next; } }; class randomRotateList : public rotatableList { public: void rotate() { if (first) { } } }; main() { srand(time(NULL)); // This initialises the random number generator printf("Creating a singleRotateList and rotating it:\n"); singleRotateList* l=new singleRotateList(); l->addFirst(3); l->addFirst(4); l->addFirst(5); l->addFirst(6); l->print(); l->rotate(); printf("\nAfter rotation:\n"); l->print(); delete l; printf("\nNow using a randomRotateList:\n"); }
Last edited by Corum; Aug 5th, 2008 at 3:12 pm.
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).
Hope this helps.
[edit] BTW. What if your circular list has just one item? (It will get printed twice.)
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)
void print() { listitem *c=first; if (c) { printf("%d\n", *c); c=first=first->next; do{ printf("%d\n", *c); c=c->next; }while (c != first); } }
[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.
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
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!
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)
#include <stdio.h> #include <stdlib.h> #include <time.h> struct listitem { int contents; struct listitem* next; struct listitem* prev; }; class rotatableList { protected: listitem *first; int size; public: rotatableList() { first=NULL; size=0; } void addFirst(int i) { struct listitem* newItem=new listitem(); newItem->contents=i; if (!first) { newItem->next=newItem; newItem->prev=newItem; first=newItem; } else { newItem->next=first; newItem->prev=first->prev; first->prev->next=newItem; first->prev=newItem; first=newItem; } size++; } void print() { listitem *c=first; if (c) { printf("%d\n", *c); c=first->next; do{ printf("%d\n", *c); c=c->next; }while (c != first); } } void rotate() { if (first) first=first->next; } }; class singleRotateList : public rotatableList { public: }; class randomRotateList : public rotatableList { public: void rotate() { if (first) { int numberOfTimes = rand() % 4; printf("The list will be rotated %d times.\n", numberOfTimes); for (int i = 0; i<numberOfTimes; i++){ first=first->next; } } } }; --------------------------------------------------------------------------------- main() { srand(time(NULL)); // This initialises the random number generator printf("Creating a singleRotateList and rotating it:\n"); rotatableList* l=new singleRotateList(); l->addFirst(3); l->addFirst(4); l->addFirst(5); l->addFirst(6); l->print(); l->rotate(); printf("\nAfter rotation:\n"); l->print(); delete l; printf("\nNow using a randomRotateList:\n"); l=new randomRotateList(); l->addFirst(3); l->addFirst(4); l->addFirst(5); l->addFirst(6); l->print(); l->rotate(); printf("\nAfter rotation:\n"); l->print(); --------------------------------------------------------------------------------- }
Use magical word virtual in
and try again...
C++ Syntax (Toggle Plain Text)
class rotatableList { ... virtual void rotate () ... ... };
Last edited by ArkM; Aug 6th, 2008 at 11:32 am.
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- Doubly linked circular list (C)
- Circular linked list (C)
- linked list question (C)
- circular linked lists (C++)
- circular refrence (C++)
Other Threads in the C++ Forum
- Previous Thread: trouble with multi text char
- Next Thread: Linking error in the compiler given by topcoder. Plz help me out guys.
Views: 614 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int lazy link linker list loop loops map math matrix member memory network newbie number object objects opengl operator output parameter pointer pointers problem program programming project python random read reading recursion recursive reference return server sort spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock wxwidgets






