new: graph module

This commit is contained in:
Simone Margaritelli 2021-04-07 17:04:42 +02:00
parent 6aa8f45d20
commit db275429c2
7 changed files with 1078 additions and 0 deletions

47
modules/graph/stack.go Normal file
View file

@ -0,0 +1,47 @@
package graph
import "sync"
type element struct {
data interface{}
next *element
}
type stack struct {
lock *sync.Mutex
head *element
Size int
}
func (stk *stack) Push(data interface{}) {
stk.lock.Lock()
element := new(element)
element.data = data
temp := stk.head
element.next = temp
stk.head = element
stk.Size++
stk.lock.Unlock()
}
func (stk *stack) Pop() interface{} {
if stk.head == nil {
return nil
}
stk.lock.Lock()
r := stk.head.data
stk.head = stk.head.next
stk.Size--
stk.lock.Unlock()
return r
}
func NewStack() *stack {
stk := new(stack)
stk.lock = &sync.Mutex{}
return stk
}