push all exercices

This commit is contained in:
lbenedar
2026-02-21 14:09:13 +03:00
commit a94d994b8c
72 changed files with 787 additions and 0 deletions

BIN
ch8/3/ch8_3.exe Normal file

Binary file not shown.

91
ch8/3/ch8_3.go Normal file
View File

@@ -0,0 +1,91 @@
package main
import "fmt"
type Node[T comparable] struct {
val T
next *Node[T]
}
func (elem *Node[T]) Add(newVal T) {
currPos := elem.GoToEnd()
currPos.next = &Node[T]{val: newVal, next: nil}
}
func (elem *Node[T]) GoToEnd() *Node[T] {
currPos := elem
for currPos.next != nil {
currPos = currPos.next
}
return currPos
}
func (elem *Node[T]) GoToPosition(len int) *Node[T] {
currPos := elem
index := 0
for currPos.next != nil && index != len {
currPos = currPos.next
index++
}
return currPos
}
func (elem *Node[T]) Insert(newVal T, index int) {
currPos := elem.GoToPosition(index - 1)
nextNode := currPos.next
currPos.next = &Node[T]{val: newVal, next: nextNode}
}
func (elem Node[T]) Index(cmpVal T) int {
currPos := &elem
index := 0
for currPos != nil && currPos.val != cmpVal {
currPos = currPos.next
index++
}
if currPos == nil {
return -1
}
return index
}
func (elem Node[T]) GoToEndWithOutput() {
currPos := elem
for currPos.next != nil {
fmt.Println(currPos)
currPos = *currPos.next
}
fmt.Println(currPos)
}
func main() {
linkedList := Node[int]{val: 12}
linkedList.Add(4)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(7)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(22)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(65)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(2)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(3)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Add(1)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Insert(10, 4)
linkedList.GoToEndWithOutput()
fmt.Println()
linkedList.Insert(11, 5)
linkedList.GoToEndWithOutput()
fmt.Println()
fmt.Println(linkedList.Index(11))
}

3
ch8/3/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module ch8_3
go 1.25.0