How to implement a stack using a linked list?

92 views

How to implement a stack using a linked list?

How to implement a stack using a linked list?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

Use a linked list with push and pop operations.

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class Stack:
    def __init__(self):
        self.top = None

    def push(self, value):
        new_node = Node(value)
        new_node.next = self.top
        self.top = new_node

    def pop(self):
        if self.top is None:
            return None
        popped = self.top.value
        self.top = self.top.next
        return popped

stack = Stack()
stack.push(1)
stack.push(2)
print(stack.pop())  # Output: 2

solveurit24@gmail.com Changed status to publish February 13, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.