Ring Data Structure (a ring of beads)

Introduction

User objects are held in a ring that has no begining or end. There is a "pointer" that "points" to one of the objects. The "pointer" can be move around the ring.

Beads hold the objects the user inserts into the ring. The ring can contain a mixture of objects because the ring does not have knowledge of object internals. Objects are the responsibility of the user.

The ring's beads are not maintained in any particular order. They are in the order the user inserts them.

Why have beads?

Beads allow the ring to know nothing about the objects it contains and therefore imposes no requirements on the objects in the ring.

Note: I have no idea what this data structure could be use for. I did it because it was fun.

Diagram

image missing

Design

In this code the class Bead is external to the class Ring. Bead could be an internal class of the Ring class. Making the class Bead internal means the user only deals with their objects and the Ring class. This could make things simpler for the user?

Code Example

Insert and Display Some Objects

rng = Ring() rng.insert(obj1) display_obj(rng.fetch(),'--- cur obj -----') rng.insert(obj2,True) rng.forward() display_obj(rng.fetch(),'--- cur obj -----') rng.backward() display_obj(rng.fetch(),'--- cur obj -----') rng.insert(obj3,False) rng.backward() display_obj(rng.fetch(),'--- cur obj -----')

Files

File NameDescription
ring.py Ring program.
ring_multi_obj_types.py Ring example with multiple object types.
ring.html PyDoc file.

How to Create PyDoc Files

To create an html file:

   pydoc -w ring

To run a web server (port 2001) for the documentation
for the modules in the current directory:

   python3 -m pydoc -p 2001