Created
March 11, 2022 20:26
-
-
Save ibeauregard/80db72ff2eab2099c2eee5462f2b0b63 to your computer and use it in GitHub Desktop.
A stack implementation using only one queue and whose methods only use standard queue operations (enqueue, dequeue, peek, size)
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
from collections import deque | |
from functools import wraps | |
class QueueStack: | |
def __init__(self): | |
self.q = deque() | |
def __repr__(self): | |
return f'QueueStack(q={self.q})' | |
def push(self, x): | |
self.q.append(x) | |
for _ in range(len(self.q) - 1): | |
self.q.append(self.q.popleft()) | |
def pop(self): | |
return self.q.popleft() | |
def top(self): | |
return self.q[0] | |
def empty(self): | |
return not self.q |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment