Dequeue removes and returns the item from the front of the queue. Returns an error if the queue is empty.
()
| 55 | // Dequeue removes and returns the item from the front of the queue. |
| 56 | // Returns an error if the queue is empty. |
| 57 | func (cq *CircularQueue[T]) Dequeue() (T, error) { |
| 58 | if cq.IsEmpty() { |
| 59 | var zeroValue T |
| 60 | return zeroValue, errors.New("queue is empty") |
| 61 | } |
| 62 | retVal := cq.items[cq.front] |
| 63 | if cq.front == cq.rear { |
| 64 | cq.front = -1 |
| 65 | cq.rear = -1 |
| 66 | } else { |
| 67 | cq.front = (cq.front + 1) % cq.size |
| 68 | } |
| 69 | return retVal, nil |
| 70 | } |
| 71 | |
| 72 | // IsFull checks if the queue is full. |
| 73 | func (cq *CircularQueue[T]) IsFull() bool { |