Complete the code to declare the stack array with a fixed size of 5.
var stack [[1]]intThe stack array is declared with size 5 to hold up to 5 elements.
Complete the code to initialize the top index of the stack to -1.
var top = [1]The top index is initialized to -1 to indicate the stack is empty.
Fix the error in the Push function to correctly add an element to the stack.
func Push(value int) {
if top == len(stack)-1 {
fmt.Println("Stack Overflow")
return
}
top = top [1] 1
stack[top] = value
}We increase top by 1 before adding the new value to the stack.
Fill both blanks to complete the Pop function that removes and returns the top element.
func Pop() int {
if top == [1] {
fmt.Println("Stack Underflow")
return -1
}
value := stack[top]
top = top [2] 1
return value
}Check if top is -1 to detect empty stack. Decrease top by 1 after popping.
Fill both blanks to complete the Peek function that returns the top element without removing it.
func Peek() int {
if top == [1] {
fmt.Println("Stack is empty")
return [2]
}
return stacktop
}Check if top is -1 for empty stack. Return 0 as default.
