Queue is an abstract data type which keeps all data in order by following the rule FIFO (Fist In, Fist, Out). This colletion data type has two principal methods. The firts one is enqueue or add, which places an new element in the end of the struct. The other one is called dequeue, pop or even remove, which takes out the first element and return it.
Queue is used in situations that the order cares and you won't need to access the data at the middle or at the end of the struct. If you need get the elements at the same order you placed than, so Queue is a good colletion data type.
| Algorithm | Average | Worst case |
|---|---|---|
| Memory Space | O(n) | O(n) |
| Search | O(n) | O(n) |
| Insert | O(1) | O(1) |
| Delete | O(1) | O(1) |
All data placed in the queue is first stored in a Cell class, which has value and next with attributes. When a new data is
placed, this data is now a Cell object and the next attribute of the last element added/placed now is pointing to this new Cell.
The Queue class only points to the first and last Cell to favor the queue and dequeue methods.
Import
from AbstractDataTypes import QueueNew Queue instance
max_size specific the how many elements Queue can have. Queue(max_size=0) or Queue() create a ilimited queue.
You can also initialize Queue with values, just passing an __ iter __ object.
qu = Queue()
# < <
qu = Queue([0, 2, 4, 6, 8])
# <0, 2, 4, 6, 8<Enqueue
qu.enqueue(10)
# <0, 2, 4, 6, 8, 10<Dequeue
qu.dequeue()
# <2, 4, 6, 8, 10<