Skip to content

Instantly share code, notes, and snippets.

@talhaahussain
Last active June 4, 2024 02:00
Show Gist options
  • Save talhaahussain/5d3960c1831328745c608682ae7c0c58 to your computer and use it in GitHub Desktop.
Save talhaahussain/5d3960c1831328745c608682ae7c0c58 to your computer and use it in GitHub Desktop.
stack.py -- An object-oriented, first principles implementation of a stack data structure, written in Python.
class Stack:
def __init__(self):
self.stack = []
self.stacklen = int(input(print("How long should the stack be?")))
def push(self):
if len(self.stack) == self.stacklen:
self.full()
else:
self.stack.append(int(input("Enter a value to append.\n")))
def pop(self):
if len(self.stack) == 0:
self.empty()
else:
self.stack.pop()
def peek(self):
return self.stack[0]
def full(self):
if len(self.stack) == self.stacklen:
return "Full."
else:
return "Not full."
def empty(self):
if len(stack) == 0:
return "Empty."
else:
return "Not empty."
def showme(self):
return self.stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment