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)

Python list implements stack and queue

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.

  1. Implementing a Stack Using Python List:

    • Use the 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)
    
  2. Using Lists as a Stack and Queue in Python:

    • A list can be used as both a stack and a queue. Use 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)
    
  3. Stack and Queue Operations with Python Lists:

    • Use list methods such as 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
    
  4. Push and Pop Operations with Python List Stack:

    • Utilize append() for push operations and pop() for pop operations.
    # Example
    stack = []
    stack.append(1)  # Push
    stack.append(2)
    popped_element = stack.pop()  # Pop
    
  5. Enqueue and Dequeue Operations with Python List Queue:

    • Use 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
    
  6. Stack and Queue Implementation Using Python Lists:

    • Implement a stack and a queue using list methods.
    # 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
    
  7. Building a Stack and Queue with Python Lists:

    • Build a stack and a queue using list methods for push, pop, enqueue, and dequeue operations.
    # 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
    
  8. Python List as a LIFO and FIFO Data Structure:

    • A list can function as both a Last In, First Out (LIFO) stack and a First In, First Out (FIFO) queue.
    # 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