Python/data_structures/stacks/__init__.py

23 lines
467 B
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
class Stack:
2019-10-05 05:14:13 +00:00
def __init__(self):
self.stack = []
self.top = 0
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
def is_empty(self):
return self.top == 0
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
def push(self, item):
if self.top < len(self.stack):
self.stack[self.top] = item
else:
self.stack.append(item)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
self.top += 1
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
def pop(self):
if self.is_empty():
return None
else:
self.top -= 1
return self.stack[self.top]