Integer Stack
/* * A struct and methods to linked stack of integers. */ package main /* * Node for the linked structure. */ type Node struct { data int next *Node } // Establish a type name alias. IntStack is just a pointer to Node. type IntStack struct { head *Node } // Push method for the stack. Notice that the receiver, stack, is // effectively a pointer to a pointer to Node. func (stack *IntStack) push(i int) { nn := new(Node) nn.data = i 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 *IntStack) pop() (v int, ok bool) { if stack.head == nil { return 0, false } else { v = stack.head.data stack.head = stack.head.next ok = true return } } // Empty method. func (stack *IntStack) empty() bool { return stack.head == nil }