Complete the code to push an element onto an array-based stack.
func (s *ArrayStack) Push(value int) {
if s.top == len(s.data) {
s.data = append(s.data, [1])
}
s.data[s.top] = value
s.top++
}When the array is full, we append a zero value to extend it before assigning the new value.
Complete the code to pop an element from a linked list stack.
func (s *LinkedListStack) Pop() (int, bool) {
if s.top == nil {
return 0, false
}
value := s.top.value
s.top = [1]
return value, true
}To pop, we move the top pointer to the next node in the linked list.
Fix the error in the array stack's IsEmpty method.
func (s *ArrayStack) IsEmpty() bool {
return [1]
}The stack is empty when the top index is zero, not when the data slice length is zero.
Fill both blanks to correctly initialize a linked list stack node and push it.
func (s *LinkedListStack) Push(value int) {
node := &Node{value: value, [1]: s.top}
s.top = [2]
}The new node's next points to the current top, then top is updated to the new node.
Fill all three blanks to create a map of stack size and capacity and check if array stack is full.
sizes := map[string]int{"size": [1], "capacity": [2]
func (s *ArrayStack) IsFull() bool {
return s.top [3] len(s.data)
}The array stack size is s.top, capacity is the length of its data slice, and IsFull checks if top equals data length.
