Enqueue adds an item to the rear of the queue. Returns an error if the queue is full.
(item T)
| 41 | // Enqueue adds an item to the rear of the queue. |
| 42 | // Returns an error if the queue is full. |
| 43 | func (cq *CircularQueue[T]) Enqueue(item T) error { |
| 44 | if cq.IsFull() { |
| 45 | return errors.New("queue is full") |
| 46 | } |
| 47 | if cq.IsEmpty() { |
| 48 | cq.front = 0 |
| 49 | } |
| 50 | cq.rear = (cq.rear + 1) % cq.size |
| 51 | cq.items[cq.rear] = item |
| 52 | return nil |
| 53 | } |
| 54 | |
| 55 | // Dequeue removes and returns the item from the front of the queue. |
| 56 | // Returns an error if the queue is empty. |