github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/stack/stack.gno (about)

     1  package stack
     2  
     3  type Stack struct {
     4  	top    *node
     5  	length int
     6  }
     7  
     8  type node struct {
     9  	value interface{}
    10  	prev  *node
    11  }
    12  
    13  func New() *Stack {
    14  	return &Stack{nil, 0}
    15  }
    16  
    17  func (s *Stack) Len() int {
    18  	return s.length
    19  }
    20  
    21  func (s *Stack) Top() interface{} {
    22  	if s.length == 0 {
    23  		return nil
    24  	}
    25  	return s.top.value
    26  }
    27  
    28  func (s *Stack) Pop() interface{} {
    29  	if s.length == 0 {
    30  		return nil
    31  	}
    32  
    33  	node := s.top
    34  	s.top = node.prev
    35  	s.length -= 1
    36  	return node.value
    37  }
    38  
    39  func (s *Stack) Push(value interface{}) {
    40  	node := &node{value, s.top}
    41  	s.top = node
    42  	s.length += 1
    43  }