JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
about 2 hours ago2 views
👨‍💻Programming
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


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

    def is_empty(self):
        return self.top is None

    def push(self, data):
        new_node = Node(data)
        new_node.next = self.top
        self.top = new_node
        print(f"{data} pushed into stack")

    def pop(self):
        if self.is_empty():
            print("stack underflow")
            return None
        popped_data = self.top.data
        self.top = self.top.next
        print(f"popped: {popped_data}")
        return popped_data

    def peek(self):
        if self.is_empty():
            print("stack is empty")
            return None
        print(f"top element: {self.top.data}")
        return self.top.data

    def display(self):
        if self.is_empty():
            print("stack is empty")
        else:
            temp = self.top
            print("stack:", end=" ")
            while temp:
                print(temp.data, end=" ")
                temp = temp.next
            print()


# Test the stack
s = Stack()
s.push(10)
s.push(20)
s.push(30)
s.display()
s.peek()
s.pop()
s.display()
← Back to timeline