mirror of
https://github.com/bettercap/bettercap
synced 2025-07-14 00:53:46 -07:00
new: graph module
This commit is contained in:
parent
6aa8f45d20
commit
db275429c2
7 changed files with 1078 additions and 0 deletions
47
modules/graph/stack.go
Normal file
47
modules/graph/stack.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue