Refactoring : isolate stack manipulation into their own package

This commit is contained in:
Julien Dessaux 2021-11-12 15:26:16 +01:00
parent 71099a8a4e
commit c913c2ae46
7 changed files with 195 additions and 146 deletions

View file

@ -1,54 +1,13 @@
package pointer
type Stack struct {
size int
height int
data []int
next *Stack // Pointer to the next element in the stack stack
func (ss *StackStack) Pop() int {
return ss.head.Pop()
}
func NewStack() *Stack {
return &Stack{
size: 32,
height: 0,
data: make([]int, 32),
next: nil,
}
func (ss *StackStack) Push(v int) {
ss.head.Push(v)
}
func (s *Stack) Clear() {
s.height = 0
}
func (s *Stack) Duplicate() {
if s.height > 0 {
s.Push(s.data[s.height-1])
} else {
s.Push(0)
s.Push(0)
}
}
func (s *Stack) Pop() int {
if s.height > 0 {
s.height--
return s.data[s.height]
}
return 0
}
func (s *Stack) Push(value int) {
if s.height >= s.size {
s.size += 32
s.data = append(s.data, make([]int, 32)...)
}
s.data[s.height] = value
s.height++
}
func (s *Stack) Swap() {
a := s.Pop()
b := s.Pop()
s.Push(a)
s.Push(b)
func (s *StackStack) YCommandPick(n int, h int) {
s.head.YCommandPick(n, h)
}