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