/*
* A generalized stack. This is as close as we get to a generic stack.
*/
package main
/*
* Node for the linked structure.
*/
type Node struct {
data interface {}
next *Node
}
// This represents the stack itself.
type Stack struct {
head *Node
}
// Push method for the stack. Notice that the receiver, stack, is
// effectively a pointer to a pointer to Node.
func (stack *Stack) push(d interface {}) {
nn := new(Node)
nn.data = d
nn.next = stack.head
stack.head = nn
}
// Pop method for stack. If the stack is not empty, return the top
// item and true. If it's empty, return 0 and false, without changing
// the stack.
func (stack *Stack) pop() (v interface {}, ok bool) {
if stack.head == nil {
return nil, false
} else {
v = stack.head.data
stack.head = stack.head.next
ok = true
return
}
}
// Empty method.
func (stack *Stack) empty() bool {
return stack.head == nil
}