2015-05-15 18:08:46 +08:00
|
|
|
class Node(object):
|
2015-07-12 03:39:59 +08:00
|
|
|
|
2015-05-15 18:08:46 +08:00
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
self.next = None
|
|
|
|
|
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):
|
|
|
|
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
|
2015-05-24 05:01:45 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
def is_empty(self):
|
|
|
|
return self.peek() is None
|