Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In this tutorial, we'll show you how to implement a stack and a queue using Python lists.
Stacks and queues are two common data structures that are used to store and manage collections of items. A stack is a Last-In-First-Out (LIFO) data structure, while a queue is a First-In-First-Out (FIFO) data structure.
1. Implementing a Stack using Python List
A stack can be easily implemented using a Python list. Here's how:
class Stack: def __init__(self): self.items = [] def is_empty(self): return not bool(self.items) def push(self, item): self.items.append(item) def pop(self): if not self.is_empty(): return self.items.pop() def peek(self): if not self.is_empty(): return self.items[-1] def size(self): return len(self.items)
2. Using the Stack
Here's an example of how to use the stack:
stack = Stack() stack.push(1) stack.push(2) stack.push(3) print("Stack size:", stack.size()) # Output: Stack size: 3 print("Top element:", stack.peek()) # Output: Top element: 3 stack.pop() print("Top element:", stack.peek()) # Output: Top element: 2
3. Implementing a Queue using Python List
A queue can be implemented using a Python list, although it's not the most efficient way to do so. Here's a simple implementation:
class Queue: def __init__(self): self.items = [] def is_empty(self): return not bool(self.items) def enqueue(self, item): self.items.insert(0, item) def dequeue(self): if not self.is_empty(): return self.items.pop() def peek(self): if not self.is_empty(): return self.items[-1] def size(self): return len(self.items)
4. Using the Queue
Here's an example of how to use the queue:
queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) print("Queue size:", queue.size()) # Output: Queue size: 3 print("Front element:", queue.peek()) # Output: Front element: 1 queue.dequeue() print("Front element:", queue.peek()) # Output: Front element: 2
Note: Using a Python list to implement a queue can lead to performance issues for large queues, as inserting an element at the beginning of a list takes O(n) time. To implement a more efficient queue, you can use the collections.deque
class, which has O(1) time complexity for appending and popping elements from both ends of the deque.
Implementing a Stack Using Python List:
append()
method to push elements onto the stack, and pop()
to remove elements from the top.# Example stack = [] stack.append(1) # Push stack.append(2) popped_element = stack.pop() # Pop (Last In, First Out)
Using Lists as a Stack and Queue in Python:
append()
and pop()
for stack operations, and append()
and pop(0)
for queue operations.# Example my_list = [1, 2, 3] my_list.append(4) # Enqueue popped_element = my_list.pop(0) # Dequeue (First In, First Out)
Stack and Queue Operations with Python Lists:
append()
and pop()
for stack operations, and append()
and pop(0)
for queue operations.# Example (Stack operations) stack = [] stack.append(1) # Push popped_element = stack.pop() # Pop # Example (Queue operations) queue = [1, 2, 3] queue.append(4) # Enqueue dequeued_element = queue.pop(0) # Dequeue
Push and Pop Operations with Python List Stack:
append()
for push operations and pop()
for pop operations.# Example stack = [] stack.append(1) # Push stack.append(2) popped_element = stack.pop() # Pop
Enqueue and Dequeue Operations with Python List Queue:
append()
for enqueue operations and pop(0)
for dequeue operations.# Example queue = [1, 2, 3] queue.append(4) # Enqueue dequeued_element = queue.pop(0) # Dequeue
Stack and Queue Implementation Using Python Lists:
# Stack stack = [] stack.append(1) # Push popped_element = stack.pop() # Pop # Queue queue = [1, 2, 3] queue.append(4) # Enqueue dequeued_element = queue.pop(0) # Dequeue
Building a Stack and Queue with Python Lists:
# Stack stack = [] stack.append(1) # Push popped_element = stack.pop() # Pop # Queue queue = [1, 2, 3] queue.append(4) # Enqueue dequeued_element = queue.pop(0) # Dequeue
Python List as a LIFO and FIFO Data Structure:
# LIFO (Stack) stack = [] stack.append(1) # Push popped_element = stack.pop() # Pop # FIFO (Queue) queue = [1, 2, 3] queue.append(4) # Enqueue dequeued_element = queue.pop(0) # Dequeue