package main
import (
"fmt"
"sync"
)
type Node struct {
value int
next *Node
}
type StackOfInts struct {
head *Node
length int
sync.Mutex
}
func (s *StackOfInts) Push(n int) {
s.Lock()
defer s.Unlock()
this := s.head
s.head = &Node{
value: n,
next: this,
}
s.length++
}
func (s *StackOfInts) Pop() int {
s.Lock()
defer s.Unlock()
popped := s.head
s.head = s.head.next
s.length--
return popped.value
}
func (s *StackOfInts) Len() int {
s.Lock()
defer s.Unlock()
return s.length
}
func (s *StackOfInts) IsEmpty() bool {
s.Lock()
defer s.Unlock()
return s.head == nil
}
func NewStack() *StackOfInts {
return &StackOfInts{
head: nil,
}
}
func main() {
s := NewStack()
s.Push(10)
s.Push(20)
s.Push(30)
s.Push(40)
for s.Len() > 0 {
fmt.Println(s.Pop())
}
fmt.Println(s.IsEmpty())
}