Complete the code to define the node structure for the linked list stack.
type Node struct {
value int
next *[1]
}The next field points to the next Node in the linked list.
Complete the code to initialize an empty stack with a top pointer.
type Stack struct {
top *[1]
}The top points to the top Node in the stack.
Fix the error in the Push method to correctly add a new node on top of the stack.
func (s *Stack) Push(val int) {
newNode := &Node{value: val}
newNode.next = s.[1]
s.top = newNode
}The new node's next should point to the current top node before updating top.
Fill both blanks to complete the Pop method that removes and returns the top value.
func (s *Stack) Pop() (int, bool) {
if s.top == nil {
return 0, false
}
val := s.top.value
s.top = s.top.[1]
return val, [2]
}Pop moves the top pointer to the next node and returns true for success.
Fill all three blanks to complete the Peek method that returns the top value without removing it.
func (s *Stack) Peek() (int, bool) {
if s.top == nil {
return 0, [1]
}
return s.top.[2], [3]
}Peek returns false if empty, else returns the top value and true.
