GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — go-dsa/queue/circular_queue_using_array.go at main · spring1843/go-dsa Expand file tree/ Copy pathcircular_queue_using_array.goMore file actions 55 lines (45 loc) · 1.11 KB / Copy pathcircular_queue_using_array.goFile metadata and controls55 lines (45 loc) · 1.11 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 package queue import ( "math" "slices" ) const emptyValue = math.MinInt64 // CircularQueue is a fixed size queue that overwrites its oldest element when it is full. type CircularQueue struct { data []int size int head int tail int } // NewCircularQueue returns an empty circular queue with the given size. func NewCircularQueue(size int) *CircularQueue { return &CircularQueue{ data: slices.Repeat([]int{emptyValue}, size), size: size, head: -1, tail: 0, } } func (queue *CircularQueue) enqueue(n int) { queue.head = queue.nextCircularSlicePosition(queue.head) if queue.data[queue.head] != emptyValue { queue.tail = queue.nextCircularSlicePosition(queue.tail) } queue.data[queue.head] = n } func (queue *CircularQueue) dequeue() (int, error) { if queue.data[queue.tail] == emptyValue { return 0, ErrQueueEmpty } output := queue.data[queue.tail] queue.data[queue.tail] = emptyValue queue.tail = queue.nextCircularSlicePosition(queue.tail) return output, nil } func (queue *CircularQueue) nextCircularSlicePosition(value int) int { value++ if value == queue.size { return 0 } return value } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |