0% found this document useful (0 votes)
6 views

Class Stack

Uploaded by

gamingpubgteam
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Class Stack

Uploaded by

gamingpubgteam
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

class Stack:

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

def is_empty(self):
return self.items == []

def push(self, item):


self.items.append(item)

def pop(self):
if not self.is_empty():
return self.items.pop()
else:
print("Stack is empty")

def peek(self):
if not self.is_empty():
return self.items[-1]
else:
print("Stack is empty")

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

# Example usage:
stack = Stack()

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

print("Stack size:", stack.size()) # Output: 3


print("Top element:", stack.peek()) # Output: 3

print("Popping elements:")
print(stack.pop()) # Output: 3
print(stack.pop()) # Output: 2
print(stack.pop()) # Output: 1

print("Stack size after popping:", stack.size()) # Output: 0

You might also like