Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

CircularStack (See related posts)

This class implements a sorta circular stack. If you get the top item, it will pop off but be added to the bottom.

class CircularStack(list):

    def gettop(self):
        item = self.pop()
        self.append(item)
        return item


Comments on this post

tracyshaun posts on Mar 23, 2007 at 15:47
Here's a quick, generator-based circular stack...

def circular(L):
    while True:
        yield L[0]
        L.append(L.pop(0))


You can then use it like so:

>>> stack = circular([1,2,3])
>>> stack.next()
1
>>> stack.next()
2
>>> stack.next()
3
>>> stack.next()
4


etc.

You need to create an account or log in to post comments to this site.