/
listasLigadas.go
72 lines (56 loc) · 1.26 KB
/
listasLigadas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package datastructuresgo
import "fmt"
type NodeList struct {
value int
next *NodeList
}
type LinkedList struct {
head *NodeList
}
func (list *LinkedList) Add(value int) {
newNodeL := &NodeList{value: value, next: nil}
if list.head == nil {
list.head = newNodeL
} else {
current := list.head
for current.next != nil {
current = current.next
}
current.next = newNodeL
}
}
func (list *LinkedList) Remove(value int) {
if list.head == nil {
return
}
if list.head.value == value {
list.head = list.head.next
return
}
current := list.head
for current.next != nil && current.next.value != value {
current = current.next
}
if current.next != nil {
current.next = current.next.next
}
}
func (list *LinkedList) Print() {
current := list.head
for current != nil {
fmt.Printf("%d -> ", current.value)
current = current.next
}
fmt.Println("nil")
}
func LinkedLists() {
PrintMessage("\nListas Ligadas:")
list := LinkedList{}
list.Add(1)
list.Add(2)
list.Add(3)
list.Print() // Output: 1 -> 2 -> 3 -> nil
list.Remove(3)
list.Remove(1)
list.Print() // Output: 2 -> nil
}