Created
September 10, 2023 11:39
-
-
Save gabrielbussolo/43b7cf00ebd669a13805a220125f72d9 to your computer and use it in GitHub Desktop.
Stack implementation using array
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 "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