mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
28 lines
559 B
Python
28 lines
559 B
Python
class Node(object):
|
|
|
|
def __init__(self, data, next=None):
|
|
self.data = data
|
|
self.next = next
|
|
|
|
|
|
class Stack(object):
|
|
|
|
def __init__(self, top=None):
|
|
self.top = top
|
|
|
|
def push(self, data):
|
|
self.top = Node(data, self.top)
|
|
|
|
def pop(self):
|
|
if self.top is None:
|
|
return None
|
|
data = self.top.data
|
|
self.top = self.top.next
|
|
return data
|
|
|
|
def peek(self):
|
|
return self.top.data if self.top is not None else None
|
|
|
|
def is_empty(self):
|
|
return self.peek() is None
|