Thread: Circular Doubly Linked list

  1. #1
    Registered User
    Join Date
    Mar 2012

    Circular Doubly Linked list

    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.

    class GameEntry {
            GameEntry( const string& n="", int s=0 );
            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 {
            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
            GameEntry* cursor; // the cursor
    CircularDoublyLinkedList::CircularDoublyLinkedList() // constructor
    : cursor(NULL)
    CircularDoublyLinkedList::~CircularDoublyLinkedList() // destructor
        while (!empty())
    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;
            u->next = cursor->next;
            cursor->next = u;

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Firstly, you need to understand that a circular doubly linked list never has a NULL next or prev pointers. You initialise a node by setting both pointers equal to the address of the node itself, then when you want to merge it with a list you do:
    firstNode->prev->next = newNode->prev;
    newNode->prev->prev = firstNode->prev;
    firstNode->prev = newNode;
    newNode->next = firstNode;
    There are of course many ways to do this.

    EDIT: This way, two lists can also be merged without any modification of the code.
    Last edited by GReaper; 03-03-2012 at 03:43 AM.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing a doubly (circular) linked list
    By nkbxwb in forum C Programming
    Replies: 4
    Last Post: 10-09-2011, 05:14 PM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Circular doubly linked list with dummy header
    By mag_chan in forum C Programming
    Replies: 5
    Last Post: 10-31-2005, 08:44 AM
  4. Circular linked list
    By campermama in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2004, 11:53 AM
  5. Replies: 1
    Last Post: 02-24-2002, 06:22 AM