mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish stack solution.
This commit is contained in:
parent
08fabdf58d
commit
7a166bed67
|
@ -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
|
|
@ -137,9 +137,9 @@
|
|||
"%%writefile stack.py\n",
|
||||
"class Node(object):\n",
|
||||
"\n",
|
||||
" def __init__(self, data):\n",
|
||||
" def __init__(self, data, next=None):\n",
|
||||
" self.data = data\n",
|
||||
" self.next = None\n",
|
||||
" self.next = next\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Stack(object):\n",
|
||||
|
@ -148,21 +148,18 @@
|
|||
" self.top = top\n",
|
||||
"\n",
|
||||
" def push(self, data):\n",
|
||||
" node = Node(data)\n",
|
||||
" node.next = self.top\n",
|
||||
" node = Node(data, self.top)\n",
|
||||
" self.top = node\n",
|
||||
"\n",
|
||||
" def pop(self):\n",
|
||||
" if self.top is not None:\n",
|
||||
" data = self.top.data\n",
|
||||
" self.top = self.top.next\n",
|
||||
" return data\n",
|
||||
" return None\n",
|
||||
" if self.top is None:\n",
|
||||
" return None\n",
|
||||
" data = self.top.data\n",
|
||||
" self.top = self.top.next\n",
|
||||
" return data\n",
|
||||
"\n",
|
||||
" def peek(self):\n",
|
||||
" if self.top is not None:\n",
|
||||
" return self.top.data\n",
|
||||
" return None\n",
|
||||
" return self.top.data if self.top is not None else None\n",
|
||||
"\n",
|
||||
" def is_empty(self):\n",
|
||||
" return self.peek() is None"
|
||||
|
@ -308,21 +305,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
Loading…
Reference in New Issue
Block a user