-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.py
More file actions
26 lines (26 loc) · 911 Bytes
/
Stack.py
File metadata and controls
26 lines (26 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#stack implementation in python
#A stack is an abstract data type that uses primarily pop and push with a list of elements
#A stack is a last in first out
#Author: Joshua Steier
#I'll start by creating stack class: I need to check if it's empty or not
#Then ill peek, and I can pop and push elements in the stack
class Stack:
def __init__(self):
self.__elements= []
def isEmpty(self):
return len(self.__elements) == 0
#return the top of the stack without removal
def peek(self):
if self.isEmpty():
return None
else:
return self.__elements[len(self.elements) -1]
def push(self, value):
self.__elements.append(value)
def pop(self):
if self.isEmpty():
return None
else:
return self.__elements.pop()
def getSize(self):
return len(self.__elements)