write a python program to implement a stack using list data structure
Answers
Answered by
2
Answer:
this is the answer hope I help you
Attachments:
Answered by
0
A python program to implement a stack using a list data structure:
class Stack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
if len(self.stack) == 0:
return None
else:
return self.stack.pop()
def peek(self):
if len(self.stack) == 0:
return None
else:
return self.stack[-1]
def is_empty(self):
return len(self.stack) == 0
- You can use this stack by creating an instance of the class, for example:
s = Stack()
s.push(1)
s.push(2)
s.push(3)
print(s.pop()) # outputs 3
print(s.pop()) # outputs 2
print(s.pop()) # outputs 1
#SPJ3
Similar questions