Last active
August 29, 2015 13:57
-
-
Save zgohr/9348734 to your computer and use it in GitHub Desktop.
CodeEval's stack interface implementation in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"bufio" | |
"fmt" | |
"strings" | |
) | |
type Stack struct { | |
top *Element // Pointer to the current top element | |
size int // current size of the Stack | |
} | |
type Element struct { | |
value interface{} // dynamic type | |
next *Element // Pointer to the next element | |
} | |
func (s *Stack) Len() int { | |
return s.size | |
} | |
func (s *Stack) Push(value interface{}) { | |
s.top = &Element{value, s.top} | |
s.size++ | |
} | |
// Remove top element, return its value. If empty, return nil | |
func (s *Stack) Pop() (value interface{}) { | |
if s.size > 0 { | |
value, s.top = s.top.value, s.top.next // Sweet assignment | |
s.size-- | |
return | |
} | |
return nil | |
} | |
func main() { | |
f, _ := os.Open(os.Args[1]) | |
defer f.Close() | |
r := bufio.NewReader(f) | |
for { | |
l, err := r.ReadString('\n') | |
if err != nil { break } | |
l = strings.TrimSpace(l) | |
in_values := strings.Split(l, " ") | |
stack := new(Stack) | |
for i := 0; i < len(in_values); i++ { | |
stack.Push(in_values[i]) | |
} | |
for stack.Len() > 0 { | |
fmt.Printf("%s ", stack.Pop().(string)) | |
stack.Pop() | |
} | |
fmt.Println() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment