""" stack.py Showing how "pointers" and a singly-linked list can be used to implement a 'stack' data structure in python. ... with help from the folks in the Algorithms class Jim Mahoney | Feb 2013 | MIT license """ class Node(object): def __init__(self, data=None, down=None): self.data = data self.down = down class Stack(object): def __init__(self): self.top = None def push(self, data): """ add data to the 'top' of the stack""" new_node = Node(data) new_node.down = self.top self.top = new_node def pop(self): """ return and remove data from 'top' of the stack""" old_top = self.top self.top = old_top.down return old_top.data ## ... and then we hope old_top is garbage collected def __str__(self): """ return string version of stack and nodes """ result = "