Created
June 9, 2017 00:18
-
-
Save kumikoda/37ac3cd38eecbd96a1cfb618e7060c8c to your computer and use it in GitHub Desktop.
fifo queue in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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