Skip to content

Commit 7402a02

Browse files
authored
Updated Pop function
Added underflow condition
1 parent c0acfd4 commit 7402a02

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

data_structures/stacks/stack.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
class StackOverflowError(BaseException):
55
pass
66

7+
class StackUnderflowError(BaseException):
8+
pass
9+
710

811
class Stack:
912
"""A stack is an abstract data type that serves as a collection of
@@ -32,6 +35,8 @@ def push(self, data):
3235

3336
def pop(self):
3437
"""Pop an element off of the top of the stack."""
38+
if len(self.stack) <= 0:
39+
raise StackUnderflowError
3540
return self.stack.pop()
3641

3742
def peek(self):

0 commit comments

Comments
 (0)