data_structures.queue.circular_queue¶
Classes¶
Circular FIFO queue with a fixed capacity |
Module Contents¶
- class data_structures.queue.circular_queue.CircularQueue(n: int)¶
Circular FIFO queue with a fixed capacity
- __len__() int ¶
>>> cq = CircularQueue(5) >>> len(cq) 0 >>> cq.enqueue("A") <data_structures.queue.circular_queue.CircularQueue object at ... >>> len(cq) 1
- dequeue()¶
This function removes an element from the queue using on self.front value as an index and returns it >>> cq = CircularQueue(5) >>> cq.dequeue() Traceback (most recent call last):
…
Exception: UNDERFLOW >>> cq.enqueue(“A”).enqueue(“B”).dequeue() ‘A’ >>> (cq.size, cq.first()) (1, ‘B’) >>> cq.dequeue() ‘B’ >>> cq.dequeue() Traceback (most recent call last):
…
Exception: UNDERFLOW
- enqueue(data)¶
This function inserts an element at the end of the queue using self.rear value as an index. >>> cq = CircularQueue(5) >>> cq.enqueue(“A”) # doctest: +ELLIPSIS <data_structures.queue.circular_queue.CircularQueue object at … >>> (cq.size, cq.first()) (1, ‘A’) >>> cq.enqueue(“B”) # doctest: +ELLIPSIS <data_structures.queue.circular_queue.CircularQueue object at … >>> (cq.size, cq.first()) (2, ‘A’)
- first()¶
Returns the first element of the queue >>> cq = CircularQueue(5) >>> cq.first() False >>> cq.enqueue(“A”).first() ‘A’
- is_empty() bool ¶
Checks whether the queue is empty or not >>> cq = CircularQueue(5) >>> cq.is_empty() True >>> cq.enqueue(“A”).is_empty() False
- array¶
- front = 0¶
- n¶
- rear = 0¶
- size = 0¶