Skip to content

Instantly share code, notes, and snippets.

@RafaelBroseghini
Created October 30, 2018 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RafaelBroseghini/d534095fe9719be673ee9c6690261626 to your computer and use it in GitHub Desktop.
Save RafaelBroseghini/d534095fe9719be673ee9c6690261626 to your computer and use it in GitHub Desktop.
Stack using Linked List
"""
Implementation of a Stack (FILO) Data Structure
using a Linked List.
"""
class LinkedList(object):
class __Node(object):
def __init__(self, val):
self.val = val
self.next = None
def __str__(self):
return str(self.val)
def __init__(self, head):
self.head = LinkedList.__Node(head)
self.num_items = 1
def push(self, item):
temp = self.head
self.head = LinkedList.__Node(item)
self.head.next = temp
self.num_items += 1
def pop(self):
if self.is_empty():
raise Exception("Cannot pop from empty Stack")
self.head = self.head.next
self.num_items -= 1
def is_empty(self):
return self.num_items == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment