A stack is a last in, first out structure in which items are added from the top. Stacks are used in parsers for solving maze algorithms. Push, pop, top, and get size are the typical operations that are allowed on stack data structures. Syntax parsing, backtracking, and compiling time memory management are some real-life scenarios where stacks can be used. An example of stack implementation is as follows (stack.go):
//main package has examples shown
// in Hands-On Data Structures and algorithms with Go book
package main
// importing fmt package
import (
"fmt"
"strconv"
)
//Element class
type Element struct {
elementValue int
}
// String method on Element class
func (element *Element) String() string {
return strconv.Itoa(element.elementValue)
}
The Element class has elementValue as an attribute. The String method returns the element's elementValue.
Stacks methods...