mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added notebook solving the following: Implement a stack with push, pop, and peek methods using a linked list.
This commit is contained in:
parent
cc7c06a789
commit
72ff274ff6
|
@ -28,6 +28,10 @@ Continually updated IPython Notebooks containing coding problems and solutions (
|
|||
* [Partition a linked list around a given value](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/partition.ipynb)
|
||||
* [Add two numbers whose digits are stored in a linked list in reverse order](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/add-reverse.ipynb)
|
||||
|
||||
## Stacks and Queues
|
||||
|
||||
* [Implement a stack with push, pop, and peek methods using a linked list.](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/stack.ipynb)
|
||||
|
||||
## License
|
||||
|
||||
Copyright 2015 Donne Martin
|
||||
|
|
227
stacks-queues/stack.ipynb
Normal file
227
stacks-queues/stack.ipynb
Normal file
|
@ -0,0 +1,227 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Implement a stack with push, pop, and peek methods using a linked list.\n",
|
||||
"\n",
|
||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Pythonic-Code](#Pythonic-Code)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Clarifying Questions\n",
|
||||
"\n",
|
||||
"* None"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"### Push\n",
|
||||
"\n",
|
||||
"* Push to empty stack\n",
|
||||
"* Push to non-empty stack\n",
|
||||
"\n",
|
||||
"### Pop\n",
|
||||
"\n",
|
||||
"* Pop on empty stack\n",
|
||||
"* Pop on single element stack\n",
|
||||
"* Pop on multiple element stack\n",
|
||||
"\n",
|
||||
"### Peek\n",
|
||||
"\n",
|
||||
"* Peek on empty stack\n",
|
||||
"* Peek on one or more element stack"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"### Push\n",
|
||||
"\n",
|
||||
"* Create new node with value\n",
|
||||
"* Set node's next to top\n",
|
||||
"* Set top to node\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1)\n",
|
||||
"* Space: O(1)\n",
|
||||
"\n",
|
||||
"### Pop\n",
|
||||
"\n",
|
||||
"* If stack is empty, return NULL\n",
|
||||
"* Else \n",
|
||||
" * Save top's value\n",
|
||||
" * Set top to top.next\n",
|
||||
" * Return saved value\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1)\n",
|
||||
"* Space: O(1)\n",
|
||||
"\n",
|
||||
"### Peek\n",
|
||||
"\n",
|
||||
"* If stack is empty, return NULL\n",
|
||||
"* Else return top's value\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1)\n",
|
||||
"* Space: O(1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%writefile stack.py\n",
|
||||
"\n",
|
||||
"class Node(object):\n",
|
||||
" def __init__(self, data):\n",
|
||||
" self.data = data\n",
|
||||
" self.next = None\n",
|
||||
"\n",
|
||||
"class Stack(object):\n",
|
||||
" def __init__(self, top=None):\n",
|
||||
" self.top = top\n",
|
||||
"\n",
|
||||
" def push(self, data):\n",
|
||||
" node = Node(data)\n",
|
||||
" node.next = 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",
|
||||
"\n",
|
||||
" def peek(self):\n",
|
||||
" if self.top is not None:\n",
|
||||
" return self.top.data\n",
|
||||
" return None"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run stack.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print('Empty stack')\n",
|
||||
"stack = Stack()\n",
|
||||
"print(stack.peek())\n",
|
||||
"print(stack.pop())\n",
|
||||
"print('One element')\n",
|
||||
"top = Node(5)\n",
|
||||
"stack = Stack(top)\n",
|
||||
"print(stack.pop())\n",
|
||||
"print(stack.peek())\n",
|
||||
"print('More than one element')\n",
|
||||
"stack = Stack()\n",
|
||||
"stack.push(1)\n",
|
||||
"stack.push(2)\n",
|
||||
"stack.push(3)\n",
|
||||
"print(stack.pop())\n",
|
||||
"print(stack.peek())\n",
|
||||
"print(stack.pop())\n",
|
||||
"print(stack.peek())\n",
|
||||
"print(stack.pop())\n",
|
||||
"print(stack.peek())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Pythonic-Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Source: https://docs.python.org/2/tutorial/datastructures.html#using-lists-as-stacks\n",
|
||||
"<pre>\n",
|
||||
"5.1.1. Using Lists as Stacks\n",
|
||||
"The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:\n",
|
||||
"\n",
|
||||
">>> stack = [3, 4, 5]\n",
|
||||
">>> stack.append(6)\n",
|
||||
">>> stack.append(7)\n",
|
||||
">>> stack\n",
|
||||
"[3, 4, 5, 6, 7]\n",
|
||||
">>> stack.pop()\n",
|
||||
"7\n",
|
||||
">>> stack\n",
|
||||
"[3, 4, 5, 6]\n",
|
||||
">>> stack.pop()\n",
|
||||
"6\n",
|
||||
">>> stack.pop()\n",
|
||||
"5\n",
|
||||
">>> stack\n",
|
||||
"[3, 4]\n",
|
||||
"</pre>"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
26
stacks-queues/stack.py
Normal file
26
stacks-queues/stack.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
|
||||
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
|
Loading…
Reference in New Issue
Block a user