data_structures.stacks.stackΒΆ
AttributesΒΆ
ExceptionsΒΆ
Common base class for all exceptions |
|
Common base class for all exceptions |
ClassesΒΆ
A stack is an abstract data type that serves as a collection of |
FunctionsΒΆ
|
Module ContentsΒΆ
- exception data_structures.stacks.stack.StackOverflowErrorΒΆ
Bases:
BaseExceptionCommon base class for all exceptions
- exception data_structures.stacks.stack.StackUnderflowErrorΒΆ
Bases:
BaseExceptionCommon base class for all exceptions
- class data_structures.stacks.stack.Stack(limit: int = 10)ΒΆ
A stack is an abstract data type that serves as a collection of elements with two principal operations: push() and pop(). push() adds an element to the top of the stack, and pop() removes an element from the top of a stack. The order in which elements come off of a stack are Last In, First Out (LIFO). https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
- __bool__() boolΒΆ
- __contains__(item: T) boolΒΆ
Check if item is in stack
>>> S = Stack(3) >>> S.push(10) >>> 10 in S True
>>> S = Stack(3) >>> S.push(10) >>> 20 in S False
- __str__() strΒΆ
- is_empty() boolΒΆ
Check if a stack is empty.
>>> S = Stack() >>> S.is_empty() True
>>> S = Stack() >>> S.push(10) >>> S.is_empty() False
- is_full() boolΒΆ
>>> S = Stack() >>> S.is_full() False
>>> S = Stack(1) >>> S.push(10) >>> S.is_full() True
- peek() TΒΆ
Peek at the top-most element of the stack.
>>> S = Stack() >>> S.push(-5) >>> S.push(10) >>> S.peek() 10
>>> Stack().peek() Traceback (most recent call last): ... data_structures.stacks.stack.StackUnderflowError
- pop() TΒΆ
Pop an element off of the top of the stack.
>>> S = Stack() >>> S.push(-5) >>> S.push(10) >>> S.pop() 10
>>> Stack().pop() Traceback (most recent call last): ... data_structures.stacks.stack.StackUnderflowError
- push(data: T) NoneΒΆ
Push an element to the top of the stack.
>>> S = Stack(2) # stack size = 2 >>> S.push(10) >>> S.push(20) >>> print(S) [10, 20]
>>> S = Stack(1) # stack size = 1 >>> S.push(10) >>> S.push(20) Traceback (most recent call last): ... data_structures.stacks.stack.StackOverflowError
- size() intΒΆ
Return the size of the stack.
>>> S = Stack(3) >>> S.size() 0
>>> S = Stack(3) >>> S.push(10) >>> S.size() 1
>>> S = Stack(3) >>> S.push(10) >>> S.push(20) >>> S.size() 2
- limit = 10ΒΆ
- stack: list[T] = []ΒΆ
- data_structures.stacks.stack.test_stack() NoneΒΆ
>>> test_stack()
- data_structures.stacks.stack.TΒΆ