My problem is to add game score (i.e. player's name & score) into a circular doubly linked list.
Now I want to add a node after the cursor of the circular doubly linked list. But I'm getting error to implement my 'add' function. Error message states that 'n' & 's' was not declared in this scope. Ya I know, it wasn't declared. But how could I pass these two values (name & score)? Any suggestion would be appreciable.
Thanks in advance.
Code:
class GameEntry {
public:
GameEntry( const string& n="", int s=0 );
private:
string name;
int score;
GameEntry* prev;
GameEntry* next;
friend class CircularDoublyLinkedList; // provide CircularDoublyLinkedList access
};
GameEntry::GameEntry( const string& n, int s): name(n), score(s)
{
}
// a circular-doubly linked list
class CircularDoublyLinkedList {
public:
CircularDoublyLinkedList(); // constructor
~CircularDoublyLinkedList(); // destructor
bool empty() const; // is list empty?
const GameEntry& front() const; // element at cursor
const GameEntry& back() const; // element following cursor
void advance(); // advance cursor
void add(const GameEntry& e); // add after cursor
void remove(); // remove node after cursor
private:
GameEntry* cursor; // the cursor
};
CircularDoublyLinkedList::CircularDoublyLinkedList() // constructor
: cursor(NULL)
{
}
CircularDoublyLinkedList::~CircularDoublyLinkedList() // destructor
{
while (!empty())
remove();
}
bool CircularDoublyLinkedList::empty() const // is list empty?
{
return cursor == NULL;
}
const GameEntry& CircularDoublyLinkedList::back() const // element at cursor
{
return *cursor;
}
const GameEntry& CircularDoublyLinkedList::front() const // element following cursor
{
return *cursor->next;
}
void CircularDoublyLinkedList::advance() // advance cursor
{
cursor = cursor->next;
}
void CircularDoublyLinkedList::add(const GameEntry& e)
{ // add after cursor
GameEntry* u = new GameEntry;
u->name = n;
u->score = s;
if(cursor == NULL)
{
u->next = u;
cursor = u;
}
else
{
u->next = cursor->next;
cursor->next = u;
}
}