Skip to content

Instantly share code, notes, and snippets.

@aolufisayo
Created July 26, 2022 11:46
Show Gist options
  • Save aolufisayo/5ef639ed88fee233cc47f0851cba5550 to your computer and use it in GitHub Desktop.
Save aolufisayo/5ef639ed88fee233cc47f0851cba5550 to your computer and use it in GitHub Desktop.
implementation of a stack data structure
class Node:
def __init__(self, value):
self.value = value
self.next = None
def __str__(self):
return str({"value": self.value, "next": self.next})
class Stack:
def __init__(self, value):
new_node = Node(value)
self.top = new_node
self.height = 1
def print_list(self):
temp = self.top
if temp is not None:
print(temp.value)
temp = temp.next
def push(self, value):
new_node = Node(value)
if self.height == 0:
self.top = new_node
else:
new_node.next = self.top
self.top = new_node
self.height += 1
def pop(self):
if self.height == 0:
return None
temp = self.top
self.top = self.top.next
temp.next = None
self.height -= 1
return temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment