Skip to content

Instantly share code, notes, and snippets.

@umangshrestha
Created June 18, 2020 05:43
Show Gist options
  • Save umangshrestha/b488b75fcb04ca777663ec06b6be6824 to your computer and use it in GitHub Desktop.
Save umangshrestha/b488b75fcb04ca777663ec06b6be6824 to your computer and use it in GitHub Desktop.
class stack:
def __init__(self, size=10):
self.size = size
self.array = [None]*self.size
self.i = -1
def push(self, element):
if self.isFull():
raise Exception("Array out of bound")
self.i += 1
self.array[self.i] = element
def pop(self):
if self.isEmpty():
raise Exception("Array out of bound")
self.i -= 1
return self.array[self.i+1]
def peek(self):
return self.array[self.i]
def isEmpty(self):
return self.i <= -1
def isFull(self):
return self.i >= self.size-1
def clear(self):
self.array = [None for _ in range(self.size)]
self.i = -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment