Skip to content

Instantly share code, notes, and snippets.

@markturansky
Last active August 3, 2018 07:32
Show Gist options
  • Save markturansky/3f2273ae39f970ff56ff22a047cc6345 to your computer and use it in GitHub Desktop.
Save markturansky/3f2273ae39f970ff56ff22a047cc6345 to your computer and use it in GitHub Desktop.
GoLang Stack implementation w/ maximum capacity
package useranalytics
type Stack struct {
top *Element
size int
max int
}
type Element struct {
value interface{}
next *Element
}
func NewStack(max int) *Stack {
return &Stack{max: max}
}
// Return the stack's length
func (s *Stack) Len() int {
return s.size
}
// Return the stack's max
func (s *Stack) Max() int {
return s.max
}
// Push a new element onto the stack
func (s *Stack) Push(value interface{}) {
if s.size + 1 > s.max {
if last := s.PopLast(); last == nil {
panic("Unexpected nil in stack")
}
}
s.top = &Element{value, s.top}
s.size++
}
// Remove the top element from the stack and return it's value
// If the stack is empty, return nil
func (s *Stack) Pop() (value interface{}) {
if s.size > 0 {
value, s.top = s.top.value, s.top.next
s.size--
return
}
return nil
}
func (s *Stack) PopLast() (value interface{}) {
if lastElem := s.popLast(s.top); lastElem != nil {
return lastElem.value
}
return nil
}
//Peek returns a top without removing it from list
func (s *Stack) Peek() (value interface{}, exists bool) {
exists = false
if s.size > 0 {
value = s.top.value
exists = true
}
return
}
func (s *Stack) popLast(elem *Element) *Element {
if elem == nil {
return nil
}
// not last because it has next and a grandchild
if elem.next != nil && elem.next.next != nil {
return s.popLast(elem.next)
}
// current elem is second from bottom, as next elem has no child
if elem.next != nil && elem.next.next == nil {
last := elem.next
// make current elem bottom of stack by removing its next element
elem.next = nil
s.size--
return last
}
return nil
}
@markturansky
Copy link
Author

markturansky commented May 26, 2016

func TestStack(t *testing.T) {

    luckyNumber := 11
    stack := NewStack(luckyNumber - 1)

    // push 1 through 11
    for i := 1; i <= luckyNumber; i++ {
        stack.Push(i)
        if x, exists := stack.Peek(); x.(int) != i || !exists {
            t.Errorf("Expected %d to be on top", i)
        }
    }

    // stack should max out at 10
    if stack.Len() != (luckyNumber - 1) {
        t.Errorf("Expected %d but got %d", (luckyNumber - 1), stack.Len())
    }

    // expect to see 11 through 2, because the first one got dropped
    for i := luckyNumber; i > (luckyNumber - stack.Max()); i-- {
        if x := stack.Pop().(int); x != i {
            t.Errorf("Expected %d but got %d", i, x)
        }
    }

    // nothing should be left after popping all items from the stack
    if item, exists := stack.Peek(); exists {
        t.Errorf("Expected stack to be empty but found %#v", item)
    }

    if item := stack.PopLast(); item != nil {
        t.Errorf("Expected stack to be empty but found %#v", item)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment