0% found this document useful (0 votes)
61 views2 pages

Circular Linked List: Typedef String Elem Class Cnode (

This document describes a circular linked list data structure. It defines a CNode class to represent nodes in the linked list with a data element and next pointer. It also defines a CircleList class to manage the linked list with methods for checking if empty, accessing elements at the front and back, advancing a cursor, adding elements after the cursor, and removing elements after the cursor. The CircleList constructor initializes the cursor to NULL and the destructor removes all elements by repeatedly calling the remove method.

Uploaded by

xz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

Circular Linked List: Typedef String Elem Class Cnode (

This document describes a circular linked list data structure. It defines a CNode class to represent nodes in the linked list with a data element and next pointer. It also defines a CircleList class to manage the linked list with methods for checking if empty, accessing elements at the front and back, advancing a cursor, adding elements after the cursor, and removing elements after the cursor. The CircleList constructor initializes the cursor to NULL and the destructor removes all elements by repeatedly calling the remove method.

Uploaded by

xz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Circular Linked list

typedef string Elem; // element type


class CNode { // circularly linked list node
private:
Elem elem; // linked list element value
CNode* next; // next item in the list
friend class CircleList; // provide CircleList access
};

class CircleList { // a circularly linked list


public:
CircleList ( ); // constructor
˜CircleList( ); // destructor
bool empty( ) const; // is list empty?
const Elem& front( ) const; // element at cursor
const Elem& back( ) const; // element following cursor
void advance( ); // advance cursor
void add(const Elem& e); // add after cursor
void remove(); // remove node after cursor
private:
CNode* cursor; // the cursor
};

CircleList::CircleList( ) // constructor
: cursor(NULL) { }
CircleList::˜CircleList( ) // destructor
{ while (!empty( )) remove( ); }

void CircleList::add(const Elem& e) { // add after cursor


CNode* v = new CNode; // create a new node
v−>elem = e;
if (cursor == NULL) { // list is empty?
v−>next = v; // v points to itself
cursor = v; // cursor points to v
}else { // list is nonempty?
v−>next = cursor−>next ; // link in v after cursor
cursor−>next = v;
}
}

void CircleList::remove( ) { // remove node after cursor


CNode* old = cursor−>next; // the node being removed
if (old == cursor) // removing the only node?
cursor = NULL; // list is now empty
else
cursor−>next = old−>next; // link out the old node
delete old;} // delete the old node

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy