A ring is a data structure (object/class) that holds user objects.
User objects are held in a ring (a ring of beads) that has no beginning or end. There is a current Bead "pointer" that "points" to one of Beads in the ring. The "pointer" can be move around the ring using "forward" or "backward" methods.
Note: I have no idea what this data structure could be use for. I did it because it was fun.
Define/create a ring "ring of beads" data structure (object/class) and write a program to interactively
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.
A ring always has a "pointer" pointing to one of the ring beads or "None" if there are no ring beads. This bead is the ring's current bead.
The ring's beads are not maintained in any particular order. They are in the order the user inserts them.
Bead is a helper class for the ring class and holds the objects the user inserts into the ring.
Beads allow the ring to know nothing about the objects it contains and therefore imposes no requirements on the objects in the ring.
The student may define their own methods. These are only suggestions.
Method | Brief Description |
---|---|
__init__(self) | Initialize a ring. |
backward(self) | Move backward one bead.
Return False if there is no current bead, else return True. |
count(self) | Return the number of beads in the ring. |
delete(self) | Delete the ring's current bead. |
display_ring(self, f) | Print all of the beads in the ring.
Because the ring knows nothing about the objects it contains, the user must pass in a function to return an object's information as a string that is displayed. |
fetch(self) | Return the object contained in the ring's current bead.
If there is no current bead return None. |
forward(self) | Move forward one bead.
Return False if there is no current bead, else return True. |
insert(self, 0bj, before=True) | Create a bead
containing a user's object and insert
bead into the ring. If the 'before' flag is True, the new bead is inserted before (forward) the ring's current bead. If the 'before' flag is False, the new bead is inserted behind (backward) the ring's current bead. |
locate(self, val, f) | Locate a specific object in the ring.
If found, make the found bead the ring's current bead and return True, else return False. Because the ring knows nothing about the objects stored in it, the user must pass in a function to do the matching test. |
class Bead(): def __init__(self,obj): self.obj = obj # user object self.forward = self # forward link self.backward = self # backward link