Skip to content

Instantly share code, notes, and snippets.

@Park-Developer
Created May 22, 2021 14:25
Show Gist options
  • Save Park-Developer/2a75de45c5f1afd0f1047efc0c5342c9 to your computer and use it in GitHub Desktop.
Save Park-Developer/2a75de45c5f1afd0f1047efc0c5342c9 to your computer and use it in GitHub Desktop.
Stack With Node
class Node(object):
def __init__(self, value=None,pointer=None):
self.value=value
self.pointer=pointer
class Stack(object):
def __init__(self):
self.head=None
self.count=0
def isEmpty(self):
return not bool(self.head)
def push(self,item):
self.head=Node(item,self.head)
self.count +=1
def pop(self):
if self.count >0 and self.head:
node=self.head
self.head=node.pointer
self.count -=1
return node.value
else:
print("Stack is Empty")
def peek(self):
if self.count >0 and self.head:
return self.head.value
else:
print("Stack is empty")
def size(self):
return self.count
def _printList(self):
node=self.head
while node:
print(node.value,end="")
node=node.pointer
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment