mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-06 17:38:02 +00:00
Polish stack solution.
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
class Node(object):
|
||||
|
||||
def __init__(self, data):
|
||||
def __init__(self, data, next=None):
|
||||
self.data = data
|
||||
self.next = None
|
||||
self.next = next
|
||||
|
||||
|
||||
class Stack(object):
|
||||
@@ -11,21 +11,18 @@ class Stack(object):
|
||||
self.top = top
|
||||
|
||||
def push(self, data):
|
||||
node = Node(data)
|
||||
node.next = self.top
|
||||
node = Node(data, 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
|
||||
if self.top is None:
|
||||
return None
|
||||
data = self.top.data
|
||||
self.top = self.top.next
|
||||
return data
|
||||
|
||||
def peek(self):
|
||||
if self.top is not None:
|
||||
return self.top.data
|
||||
return None
|
||||
return self.top.data if self.top is not None else None
|
||||
|
||||
def is_empty(self):
|
||||
return self.peek() is None
|
||||
Reference in New Issue
Block a user