Lists as Stacks or Queues in Python


Use lists to implement stack (LIFO) or queue (FIFO) data structures.

Source Code

stack = []
stack.append('a')  # Push onto the stack
stack.pop()  # Pop off the stack

from collections import deque
queue = deque()
queue.append('a')  # Enqueue
queue.popleft()  # Dequeue
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments