github.com/iainanderson83/datastructures@v0.0.4-0.20191103204413-889e20b53bcf/stack/stack.go (about)

     1  package stack
     2  
     3  // Stack is an interface for a classic stack data structure
     4  type Stack interface {
     5  	Push(interface{})
     6  	Pop() interface{}
     7  	Peek() interface{}
     8  }
     9  
    10  var _ Stack = &ArrStack{}
    11  
    12  type ArrStack struct {
    13  	s []interface{}
    14  }
    15  
    16  func (a *ArrStack) Push(v interface{}) {
    17  	if v == nil {
    18  		return
    19  	}
    20  	a.s = append(a.s, v)
    21  }
    22  
    23  func (a *ArrStack) Peek() interface{} {
    24  	if len(a.s) == 0 {
    25  		return nil
    26  	}
    27  	return a.s[len(a.s)-1]
    28  }
    29  
    30  func (a *ArrStack) Pop() interface{} {
    31  	v := a.Peek()
    32  	if len(a.s) == 0 {
    33  		return v
    34  	}
    35  	a.s = a.s[:len(a.s)-1]
    36  	return v
    37  }