2015-05-15 18:08:46 +08:00
|
|
|
class Node(object):
|
2015-07-12 03:39:59 +08:00
|
|
|
|
2016-02-20 21:11:49 +08:00
|
|
|
def __init__(self, data, next=None):
|
2015-05-15 18:08:46 +08:00
|
|
|
self.data = data
|
2016-02-20 21:11:49 +08:00
|
|
|
self.next = next
|
2015-05-15 18:08:46 +08:00
|
|
|
|
2015-07-12 03:39:59 +08:00
|
|
|
|
2015-05-15 18:08:46 +08:00
|
|
|
class Stack(object):
|
2015-07-12 03:39:59 +08:00
|
|
|
|
2015-05-15 18:08:46 +08:00
|
|
|
def __init__(self, top=None):
|
|
|
|
self.top = top
|
|
|
|
|
|
|
|
def push(self, data):
|
2016-06-20 08:02:53 +08:00
|
|
|
self.top = Node(data, self.top)
|
2015-05-15 18:08:46 +08:00
|
|
|
|
|
|
|
def pop(self):
|
2016-02-20 21:11:49 +08:00
|
|
|
if self.top is None:
|
|
|
|
return None
|
|
|
|
data = self.top.data
|
|
|
|
self.top = self.top.next
|
|
|
|
return data
|
2015-05-15 18:08:46 +08:00
|
|
|
|
|
|
|
def peek(self):
|
2016-02-20 21:11:49 +08:00
|
|
|
return self.top.data if self.top is not None else None
|
2015-05-24 05:01:45 +08:00
|
|
|
|
|
|
|
def is_empty(self):
|
|
|
|
return self.peek() is None
|