2024-10-03 02:57:16 +00:00
|
|
|
package linked_list
|
|
|
|
import "core:fmt"
|
|
|
|
|
|
|
|
node :: struct($Value: typeid) {
|
|
|
|
data: Value,
|
|
|
|
next: ^node(Value),
|
|
|
|
}
|
|
|
|
|
2024-11-09 20:52:52 +00:00
|
|
|
to_string :: proc(head: ^node($T), trunc_after: int = 8) -> string {
|
2024-10-03 02:57:16 +00:00
|
|
|
curr := head
|
|
|
|
out: string
|
|
|
|
counter := 0
|
2024-11-09 20:52:52 +00:00
|
|
|
out = fmt.aprintf("[%v]", curr.data)
|
2024-10-03 02:57:16 +00:00
|
|
|
for curr.next != nil{
|
|
|
|
curr = curr.next
|
2024-11-09 20:52:52 +00:00
|
|
|
if trunc_after > 0 && counter >= trunc_after {
|
|
|
|
out = fmt.aprintf("%s -> ... -> [%v]", out, tail(curr.next))
|
2024-10-03 02:57:16 +00:00
|
|
|
break
|
|
|
|
}
|
2024-11-09 20:52:52 +00:00
|
|
|
out = fmt.aprintf("%s -> [%v]", out, curr.data)
|
2024-10-03 02:57:16 +00:00
|
|
|
counter += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
remove :: proc(head: ^^node($T), value: T) {
|
|
|
|
curr := head^
|
|
|
|
pp := head
|
|
|
|
for curr != nil && curr.data != value {
|
|
|
|
pp = &curr.next
|
|
|
|
curr = curr.next
|
|
|
|
}
|
|
|
|
|
|
|
|
if curr == nil do return
|
|
|
|
|
|
|
|
pp^ = curr.next
|
|
|
|
free(curr)
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_all :: proc(head: ^^node($T), value: T) {
|
|
|
|
curr := head^
|
|
|
|
pp := head
|
|
|
|
for {
|
|
|
|
for curr != nil && curr.data != value {
|
|
|
|
pp = &curr.next
|
|
|
|
curr = curr.next
|
|
|
|
}
|
|
|
|
if curr != nil {
|
|
|
|
pp^ = curr.next
|
|
|
|
free(curr)
|
|
|
|
curr = pp^
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prepend_value :: proc(head: ^^node($T), value: T) -> ^node(T) {
|
|
|
|
n := prepend(head)
|
|
|
|
n.data = value
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
prepend :: proc(head: ^^node($T)) -> ^node(T) {
|
|
|
|
n := new(node(T))
|
|
|
|
n.next = head^
|
|
|
|
head^ = n
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
append_value :: proc(head: ^node($T), value: T) -> ^node(T) {
|
|
|
|
n := append(head)
|
|
|
|
n.data = value
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
append :: proc(head: ^node($T)) -> ^node(T) {
|
|
|
|
curr := head
|
|
|
|
for curr.next != nil {
|
|
|
|
curr = curr.next
|
|
|
|
}
|
|
|
|
curr.next = new(node(T))
|
|
|
|
return curr.next
|
|
|
|
}
|
|
|
|
|
|
|
|
head :: proc(h: ^node($T)) -> T {
|
|
|
|
return h.data
|
|
|
|
}
|
|
|
|
|
|
|
|
tail :: proc(head: ^node($T)) -> T {
|
|
|
|
curr := head
|
|
|
|
for curr.next != nil {
|
|
|
|
curr = curr.next
|
|
|
|
}
|
|
|
|
return curr.data
|
|
|
|
}
|
|
|
|
|
|
|
|
// counting starts from 0
|
|
|
|
nth :: proc(head: ^node($T), n: u64) -> T {
|
|
|
|
curr := head
|
|
|
|
counter := 0
|
|
|
|
for curr != nil {
|
|
|
|
if counter == n {
|
|
|
|
return curr.data
|
|
|
|
}
|
|
|
|
curr = curr.next
|
|
|
|
counter += 1
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit :: proc(head: ^node($T)) {
|
|
|
|
curr := head
|
|
|
|
next: ^node(T)
|
|
|
|
for curr != nil {
|
|
|
|
next = curr.next
|
|
|
|
free(curr)
|
|
|
|
curr = next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init :: proc($T: typeid) -> (head: ^node(T)) {
|
|
|
|
return new(node(T))
|
|
|
|
}
|