Skip to content

Instantly share code, notes, and snippets.

@kumikoda
Created June 9, 2017 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumikoda/37ac3cd38eecbd96a1cfb618e7060c8c to your computer and use it in GitHub Desktop.
Save kumikoda/37ac3cd38eecbd96a1cfb618e7060c8c to your computer and use it in GitHub Desktop.
fifo queue in python
class Fifo:
def __init__(self, size):
self.size = size
self.count = 0
self.q = [None] * size
self.head = 0
self.tail = 0
def push(self, elm):
print self.count
print self.size
if (self.count == self.size):
print 'double!'
new_q = [None] * self.size * 2
for x in range(self.size):
new_q[x] = self.pop()
self.head = self.size
self.count = self.size
self.tail = 0
self.size = self.size * 2
self.q = new_q
self.q[self.head] = elm
self.head = (self.head + 1) % self.size
self.count = self.count + 1
print self.q
def pop(self):
elm = self.q[self.tail]
self.q[self.tail] = None
self.tail = (self.tail + 1) % self.size
self.count = self.count - 1
return elm
q = Fifo(4)
for i in range(10):
q.push(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment