Created
November 27, 2023 11:21
-
-
Save lalizita/dd994be918ab2e050217d148692cd03f to your computer and use it in GitHub Desktop.
Linked list example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Node struct { | |
name string | |
next *Node | |
} | |
type List struct { | |
head *Node | |
} | |
func (l *List) AddItem(name string) { | |
newNode := &Node{name: name} | |
if l.head == nil { | |
l.head = newNode | |
return | |
} | |
current := l.head | |
if current.next != nil { | |
current = current.next | |
} | |
current.next = newNode | |
} | |
func main() { | |
linkedList := &List{} | |
linkedList.AddItem("Jose") | |
linkedList.AddItem("Henrique") | |
linkedList.AddItem("Mariana") | |
fmt.Println("Lista encadeada:") | |
curr := linkedList.head | |
for curr != nil { | |
//Operação de busca O(n) | |
if curr.name == "Henrique" { | |
fmt.Printf("Nome %s presente na lista\n", curr.name) | |
} | |
fmt.Printf("%s \n", curr.name) | |
curr = curr.next | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment