Created
September 5, 2018 18:11
-
-
Save DrynnBavis/ddb075535ef531d175a04d5acf105298 to your computer and use it in GitHub Desktop.
Python3 Circular Queue Class
This file contains hidden or 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 circ_queue(object): | |
| """docstring for circ_queue.""" | |
| queue = [] | |
| head = 0 | |
| tail = 0 | |
| len = 0 | |
| def __init__(self, max_len = 8): | |
| super(circ_queue, self).__init__() | |
| self.queue = [None] * max_len | |
| self.len = max_len | |
| def enqueue(self, val): | |
| next = (self.head + 1) % self.len | |
| if self.queue[self.head] is not None: | |
| if self.queue[next] is not None: #queue is full | |
| print("queue is full, nothing added") | |
| return False | |
| else: | |
| self.head = next | |
| self.queue[self.head] = val | |
| return True | |
| def dequeue(self): | |
| next = (self.tail + 1) % self.len | |
| if self.queue[self.tail] is None: | |
| print("queue is empty, nothing removed") | |
| return None | |
| else: | |
| val = self.queue[self.tail] | |
| self.queue[self.tail] = None | |
| self.tail = next | |
| return val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment