func main() { var s stack.Stack for i := 0; i < 10; i++ { s.Push(i) } fmt.Println(s) }
func main() { fmt.Println("FILO Stack:\nPushing: \"Hello World\"") var s stack.Stack s.Push("Hello World") for i := 1; i < 6; i++ { fmt.Println("Pushing:", i) s.Push(i) } for val, ok := s.Pop(); ok; val, ok = s.Pop() { fmt.Println("Popped:", val) } fmt.Println("\n===============================\nFIFO Queue:\nPushing: \"Hello World\"") var q stack.Queue q.Push("Hello World") for i := 1; i < 6; i++ { fmt.Println("Pushing:", i) q.Push(i) } for val, ok := q.Pop(); ok; val, ok = q.Pop() { fmt.Println("Popped:", val) } fmt.Println("\n===============================\nDecorator Test:") var c interface{} c = &TypeA{7} if d, ok := c.(Stuffer); ok { fmt.Println("c is a Stuffer") d.Stuff() } else { fmt.Println("c is NOT a Stuffer") } c = &TypeB{c.(Stuffer)} if d, ok := c.(Stuffer); ok { fmt.Println("c is a Stuffer") d.Stuff() } else { fmt.Println("c is NOT a Stuffer") } }
func parse(s []string) { s = append(s, EOF) stk := new(stack.Stack) stk.Push(EOF) stk.Push(startSymbol) for { time.Sleep(time.Second) top := stk.Top() fmt.Println(stk) if top == "epsilon" { stk.Pop() } if terminals.has(top) { if top == EOF { break } if top == s[0] { stk.Pop() s = s[1:] } else { fmt.Println("invalid string") break } } if nonTerminals.has(top) { stk.Pop() for i := len(table[s[0]][top]) - 1; i >= 0; i-- { stk.Push(table[s[0]][top][i]) } } } }
func main() { var haystack stack.Stack haystack.Push("Hay") haystack.Push(-15) haystack.Push([]string{"Pin", "Clip", "Needle"}) haystack.Push(81.52) fmt.Printf("The length of the stack is %d\n", haystack.Len()) item, err := haystack.Top() if err != nil { log.Fatalln(err) } fmt.Printf("Top element of the stack is %v\n", item) fmt.Println("Items in the stack are listed below:") for { item, err = haystack.Pop() if err != nil { break } fmt.Println(item) } }