Created
October 30, 2018 05:10
-
-
Save RafaelBroseghini/d534095fe9719be673ee9c6690261626 to your computer and use it in GitHub Desktop.
Stack using Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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