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)
#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");
}