Skip to content

Instantly share code, notes, and snippets.

@gabrielbussolo
Created September 10, 2023 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielbussolo/43b7cf00ebd669a13805a220125f72d9 to your computer and use it in GitHub Desktop.
Save gabrielbussolo/43b7cf00ebd669a13805a220125f72d9 to your computer and use it in GitHub Desktop.
Stack implementation using array
package main
import "fmt"
type Stack struct {
data []int
}
func (s *Stack) Push(element int) {
s.data = append(s.data, element)
}
func (s *Stack) Pop() {
s.data = s.data[:len(s.data)-1]
}
func (s *Stack) Peek() int {
return s.data[len(s.data)-1]
}
func (s *Stack) Lookup(pos int) int {
return s.data[pos]
}
func (s *Stack) Display() {
for _, v := range s.data {
fmt.Printf(" %d ->", v)
}
fmt.Println()
}
func main() {
s := Stack{}
s.Push(1)
s.Push(2)
s.Push(3)
s.Display()
s.Pop()
s.Display()
fmt.Println(s.Peek())
fmt.Println(s.Lookup(0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment