xxxxxxxxxx
def peek_stack(stack):
if stack:
return stack[-1]
xxxxxxxxxx
class Stack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
if not self.is_empty():
return self.stack.pop()
return None
def peek(self):
if not self.is_empty():
return self.stack[-1]
return None
def is_empty(self):
return len(self.stack) == 0
# Example usage:
my_stack = Stack()
my_stack.push(5)
my_stack.push(10)
my_stack.push(3)
# Peeking at the top element
top_element = my_stack.peek()
print(top_element) # Output: 3