0% found this document useful (0 votes)
10 views3 pages

Python_Stacks_Queues

The document explains how to implement stacks and queues in Python, highlighting their respective Last In First Out (LIFO) and First In First Out (FIFO) principles. It provides examples of implementing a stack using lists, collections.deque, and queue.LifoQueue, as well as a queue using lists, collections.deque, and queue.Queue. Each implementation includes code snippets demonstrating how to push and pop elements for stacks and enqueue and dequeue elements for queues.

Uploaded by

dhiraj gholap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Python_Stacks_Queues

The document explains how to implement stacks and queues in Python, highlighting their respective Last In First Out (LIFO) and First In First Out (FIFO) principles. It provides examples of implementing a stack using lists, collections.deque, and queue.LifoQueue, as well as a queue using lists, collections.deque, and queue.Queue. Each implementation includes code snippets demonstrating how to push and pop elements for stacks and enqueue and dequeue elements for queues.

Uploaded by

dhiraj gholap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Data Structures: Stacks and Queues

1. Implementing Stack in Python

A stack is a linear data structure that follows the Last In First Out (LIFO) principle.
Python provides multiple ways to implement a stack:
1. Using a list
2. Using collections.deque
3. Using queue.LifoQueue

Stack using List

# Stack implementation using List


stack = []

# Push elements onto stack


stack.append('a')
stack.append('b')
stack.append('c')

# Pop elements from stack


print("Popped:", stack.pop()) # 'c'
print("Popped:", stack.pop()) # 'b'
print("Popped:", stack.pop()) # 'a'

Stack using collections.deque

from collections import deque

# Create a stack using deque


stack = deque()

# Push elements
stack.append('x')
stack.append('y')
stack.append('z')

# Pop elements
print("Popped:", stack.pop()) # 'z'
print("Popped:", stack.pop()) # 'y'
print("Popped:", stack.pop()) # 'x'
Stack using queue.LifoQueue

from queue import LifoQueue

# Create a stack with max size 3


stack = LifoQueue(maxsize=3)

# Push elements
stack.put('1')
stack.put('2')
stack.put('3')

# Pop elements
print("Popped:", stack.get()) # '3'
print("Popped:", stack.get()) # '2'
print("Popped:", stack.get()) # '1'

2. Implementing Queue in Python

A queue is a linear data structure that follows the First In First Out (FIFO) principle.
Python provides multiple ways to implement a queue:
1. Using a list
2. Using collections.deque
3. Using queue.Queue

Queue using List

# Queue implementation using List


queue = []

# Enqueue elements
queue.append('a')
queue.append('b')
queue.append('c')

# Dequeue elements
print("Dequeued:", queue.pop(0)) # 'a'
print("Dequeued:", queue.pop(0)) # 'b'
print("Dequeued:", queue.pop(0)) # 'c'

Queue using collections.deque


from collections import deque

# Create a queue using deque


queue = deque()

# Enqueue elements
queue.append('x')
queue.append('y')
queue.append('z')

# Dequeue elements
print("Dequeued:", queue.popleft()) # 'x'
print("Dequeued:", queue.popleft()) # 'y'
print("Dequeued:", queue.popleft()) # 'z'

Queue using queue.Queue

from queue import Queue

# Create a queue with max size 3


queue = Queue(maxsize=3)

# Enqueue elements
queue.put('1')
queue.put('2')
queue.put('3')

# Dequeue elements
print("Dequeued:", queue.get()) # '1'
print("Dequeued:", queue.get()) # '2'
print("Dequeued:", queue.get()) # '3'

You might also like