Complete the code to push an element into the queue using stack1.
func (q *Queue) Enqueue(value int) {
[1]
}The q.stack1 = append(q.stack1, value) adds an element to the top of the stack, which is needed to enqueue an element.
Complete the code to check if stack2 is empty before transferring elements.
if len(q.stack2) == [1] { for len(q.stack1) > 0 { val := q.stack1[len(q.stack1)-1] q.stack1 = q.stack1[:len(q.stack1)-1] q.stack2 = append(q.stack2, val) } }
We check if stack2 is empty by comparing its length to 0 before moving elements.
Fix the error in the Dequeue method to correctly remove and return the front element.
func (q *Queue) Dequeue() (int, bool) {
if len(q.stack2) == 0 {
for len(q.stack1) > 0 {
val := q.stack1[len(q.stack1)-1]
q.stack1 = q.stack1[:len(q.stack1)-1]
q.stack2 = append(q.stack2, val)
}
}
if len(q.stack2) == 0 {
return 0, false
}
val := q.stack2[len(q.stack2)-1]
q.stack2 = q.stack2[:[1]]
return val, true
}To remove the last element, slice stack2 up to len(q.stack2)-1.
Fill in the blank to implement the Peek method that returns the front element without removing it.
func (q *Queue) Peek() (int, bool) {
if len(q.stack2) == 0 {
for len(q.stack1) > 0 {
val := q.stack1[len(q.stack1)-1]
q.stack1 = q.stack1[:len(q.stack1)-1]
q.stack2 = append(q.stack2, val)
}
}
if len(q.stack2) == 0 {
return 0, false
}
return q.stack2[1], true
}To peek the front element, access the last element of stack2 using index len(q.stack2)-1.
Fill in the blanks to implement the IsEmpty method that returns true if the queue is empty.
func (q *Queue) IsEmpty() bool {
return len(q.stack1) [1] 0 && len(q.stack2) [2] 0
}The queue is empty only if both stacks have length equal to zero.
