MCPcopy Create free account
hub / github.com/ardanlabs/gotraining / Dequeue

Method Dequeue

topics/go/algorithms/data/queue/queue.go:74–104  ·  view source on GitHub ↗

Dequeue removes data into the queue if data exists.

()

Source from the content-addressed store, hash-verified

72
73// Dequeue removes data into the queue if data exists.
74func (q *Queue) Dequeue() (*Data, error) {
75
76 // If the front and end are the same, the
77 // queue is empty
78 // EF - (Empty)
79 // [ ][ ][ ]
80 if q.front == q.end {
81 return nil, errors.New("queue is empty")
82 }
83
84 var data *Data
85 switch {
86 case q.end == len(q.data):
87
88 // If we are at the end of the capacity, then
89 // circle back to the beginning of the capacity by
90 // moving the end pointer to the beginning.
91 q.end = 0
92 data = q.data[q.end]
93 default:
94
95 // Remove the data from the current end position
96 // and then move the end pointer.
97 data = q.data[q.end]
98 q.end++
99 }
100
101 q.Count--
102
103 return data, nil
104}
105
106// Operate accepts a function that takes data and calls
107// the specified function for every piece of data found.

Callers 3

TestDequeueFunction · 0.45
TestEnqueueFullFunction · 0.45
TestDequeueEmptyFunction · 0.45

Calls

no outgoing calls

Tested by 3

TestDequeueFunction · 0.36
TestEnqueueFullFunction · 0.36
TestDequeueEmptyFunction · 0.36