Complete the code to remove an element from the front of the dequeue.
func (dq *Dequeue) RemoveFront() int {
if dq.IsEmpty() {
return -1
}
frontElement := dq.data[dq.front]
dq.front = dq.front [1] 1
return frontElement
}To remove from the front, we move the front pointer forward by adding 1.
Complete the code to check if the dequeue is empty.
func (dq *Dequeue) IsEmpty() bool {
return dq.front [1] dq.rear
}The dequeue is empty when front and rear pointers are equal.
Fix the error in updating the rear pointer after removing an element from the rear.
func (dq *Dequeue) RemoveRear() int {
if dq.IsEmpty() {
return -1
}
rearElement := dq.data[dq.rear-1]
dq.rear = dq.rear [1] 1
return rearElement
}After removing from rear, rear pointer moves backward by subtracting 1.
Fill both blanks to correctly add an element to the front of the dequeue.
func (dq *Dequeue) AddFront(value int) {
if dq.IsFull() {
return
}
dq.front = dq.front [1] 1
dq.data[dq.front] = value
dq.rear = dq.rear [2] 0
}To add at front, front pointer moves backward by subtracting 1. Rear pointer remains unchanged, so '=' is used (rear = rear + 0).
Fill all three blanks to correctly add an element to the rear of the dequeue and update pointers.
func (dq *Dequeue) AddRear(value int) {
if dq.IsFull() {
return
}
dq.data[dq.rear] = value
dq.rear = dq.rear [1] 1
dq.front = dq.front [2] 0
_ = dq.rear [3] dq.front
}Adding at rear means rear pointer moves forward by adding 1. Front pointer remains unchanged, so '=' is used (front = front + 0). The last line is a dummy expression using '+' to avoid unused variable error.
