Added notebook solving the following: Implement a stack with push, pop, and peek methods using a linked list.

This commit is contained in:
Donne Martin
2015-05-15 06:08:46 -04:00
parent cc7c06a789
commit 72ff274ff6
3 changed files with 257 additions and 0 deletions

26
stacks-queues/stack.py Normal file
View 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