data_structures.stacks.stack_with_singly_linked_list

A Stack using a linked list like structure

Attributes

T

Classes

LinkedStack

Linked List Stack implementing push (to top),

Node

Module Contents

class data_structures.stacks.stack_with_singly_linked_list.LinkedStack

Bases: Generic[T]

Linked List Stack implementing push (to top), pop (from top) and is_empty

>>> stack = LinkedStack()
>>> stack.is_empty()
True
>>> stack.push(5)
>>> stack.push(9)
>>> stack.push('python')
>>> stack.is_empty()
False
>>> stack.pop()
'python'
>>> stack.push('algorithms')
>>> stack.pop()
'algorithms'
>>> stack.pop()
9
>>> stack.pop()
5
>>> stack.is_empty()
True
>>> stack.pop()
Traceback (most recent call last):
    ...
IndexError: pop from empty stack
__iter__() collections.abc.Iterator[T]
__len__() int
>>> stack = LinkedStack()
>>> len(stack) == 0
True
>>> stack.push("c")
>>> stack.push("b")
>>> stack.push("a")
>>> len(stack) == 3
True
__str__() str
>>> stack = LinkedStack()
>>> stack.push("c")
>>> stack.push("b")
>>> stack.push("a")
>>> str(stack)
'a->b->c'
clear() None
>>> stack = LinkedStack()
>>> stack.push("Java")
>>> stack.push("C")
>>> stack.push("Python")
>>> str(stack)
'Python->C->Java'
>>> stack.clear()
>>> len(stack) == 0
True
is_empty() bool
>>> stack = LinkedStack()
>>> stack.is_empty()
True
>>> stack.push(1)
>>> stack.is_empty()
False
peek() T
>>> stack = LinkedStack()
>>> stack.push("Java")
>>> stack.push("C")
>>> stack.push("Python")
>>> stack.peek()
'Python'
pop() T
>>> stack = LinkedStack()
>>> stack.pop()
Traceback (most recent call last):
    ...
IndexError: pop from empty stack
>>> stack.push("c")
>>> stack.push("b")
>>> stack.push("a")
>>> stack.pop() == 'a'
True
>>> stack.pop() == 'b'
True
>>> stack.pop() == 'c'
True
push(item: T) None
>>> stack = LinkedStack()
>>> stack.push("Python")
>>> stack.push("Java")
>>> stack.push("C")
>>> str(stack)
'C->Java->Python'
top: Node[T] | None = None
class data_structures.stacks.stack_with_singly_linked_list.Node(data: T)

Bases: Generic[T]

__str__() str
data
next: Node[T] | None = None
data_structures.stacks.stack_with_singly_linked_list.T