Skip to content

Instantly share code, notes, and snippets.

@senapk
Created May 19, 2025 17:25
Show Gist options
  • Save senapk/e30fbf8506514072a98b59307cdc0137 to your computer and use it in GitHub Desktop.
Save senapk/e30fbf8506514072a98b59307cdc0137 to your computer and use it in GitHub Desktop.
Exemplo de Lista Ligada Parametrizada com Root e Iterador
package main
import "fmt"
type Node[T comparable] struct {
value T
next *Node[T]
prev *Node[T]
root *Node[T]
}
func (n *Node[T]) Next() *Node[T] {
if n.next != n.root {
return n.next
}
return nil
}
type LList[T comparable] struct {
root *Node[T]
}
func NewList[T comparable]() *LList[T] {
root := &Node[T]{}
root.next = root
root.prev = root
root.root = root
return &LList[T]{root: root}
}
func (ll *LList[T]) PushBack(value T) {
node := &Node[T]{
value: value,
next: ll.root,
prev: ll.root.prev,
root: ll.root,
}
ll.root.prev.next = node
ll.root.prev = node
}
func (ll *LList[T]) Front() *Node[T] {
if ll.root.next == ll.root {
return nil
}
return ll.root.next
}
func main() {
ll := NewList[float32]()
for i := range 10 {
ll.PushBack(float32(i) + 0.01)
}
node := ll.Front()
for node != nil {
fmt.Println(node.value)
node = node.Next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment