data_structures.queue.queue_by_two_stacks

Queue implementation using two stacks

Attributes

_T

Classes

QueueByTwoStacks

Module Contents

class data_structures.queue.queue_by_two_stacks.QueueByTwoStacks(iterable: collections.abc.Iterable[_T] | None = None)

Bases: Generic[_T]

__len__() int
>>> len(QueueByTwoStacks())
0
>>> from string import ascii_lowercase
>>> len(QueueByTwoStacks(ascii_lowercase))
26
>>> queue = QueueByTwoStacks()
>>> for i in range(1, 11):
...     queue.put(i)
...
>>> len(queue)
10
>>> for i in range(2):
...   queue.get()
1
2
>>> len(queue)
8
__repr__() str
>>> queue = QueueByTwoStacks()
>>> queue
Queue(())
>>> str(queue)
'Queue(())'
>>> queue.put(10)
>>> queue
Queue((10,))
>>> queue.put(20)
>>> queue.put(30)
>>> queue
Queue((10, 20, 30))
get() _T

Get item from the Queue

>>> queue = QueueByTwoStacks((10, 20, 30))
>>> queue.get()
10
>>> queue.put(40)
>>> queue.get()
20
>>> queue.get()
30
>>> len(queue)
1
>>> queue.get()
40
>>> queue.get()
Traceback (most recent call last):
    ...
IndexError: Queue is empty
put(item: _T) None

Put item into the Queue

>>> queue = QueueByTwoStacks()
>>> queue.put(10)
>>> queue.put(20)
>>> len(queue)
2
>>> queue
Queue((10, 20))
_stack1: list[_T]
_stack2: list[_T] = []
data_structures.queue.queue_by_two_stacks._T