Friday 10 November 2023

Get Started with Stacks and Queues in Python

 


Stacks and queues are fundamental data structures used in computer science to manage and organize data. Let's get started with stacks and queues in Python.

Stacks:

A stack is a Last In, First Out (LIFO) data structure, where the last element added is the first one to be removed. Think of it like a stack of plates - you can only take the top plate off.

Implementation in Python:

class Stack:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return len(self.items) == 0

    def push(self, item):
        self.items.append(item)

    def pop(self):
        if not self.is_empty():
            return self.items.pop()
        else:
            raise IndexError("pop from an empty stack")

    def peek(self):
        if not self.is_empty():
            return self.items[-1]
        else:
            raise IndexError("peek from an empty stack")

    def size(self):
        return len(self.items)

Example usage:

stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)

print("Stack:", stack.items)

print("Pop:", stack.pop())
print("Stack after pop:", stack.items)

print("Peek:", stack.peek())
print("Stack size:", stack.size())

Queues:

A queue is a First In, First Out (FIFO) data structure, where the first element added is the first one to be removed. Think of it like a queue of people waiting in line.

Implementation in Python:

class Queue:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return len(self.items) == 0

    def enqueue(self, item):
        self.items.append(item)

    def dequeue(self):
        if not self.is_empty():
            return self.items.pop(0)
        else:
            raise IndexError("dequeue from an empty queue")

    def front(self):
        if not self.is_empty():
            return self.items[0]
        else:
            raise IndexError("front from an empty queue")

    def size(self):
        return len(self.items)


Example usage:

queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

print("Queue:", queue.items)

print("Dequeue:", queue.dequeue())
print("Queue after dequeue:", queue.items)

print("Front:", queue.front())
print("Queue size:", queue.size())

These basic implementations should get you started with stacks and queues in Python. Feel free to modify and expand upon them based on your specific needs.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (117) C (77) C# (12) C++ (82) Course (62) Coursera (179) coursewra (1) Cybersecurity (22) data management (11) Data Science (95) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (748) Python Coding Challenge (221) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses