Skip to content

Commit 70d049e

Browse files
committed
linkedlist
1 parent 79ccad0 commit 70d049e

File tree

5 files changed

+216
-128
lines changed

5 files changed

+216
-128
lines changed

examples/linkedlist/linkedlist_examples.go

Lines changed: 149 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,164 @@ import (
88
)
99

1010
func main() {
11-
// Linear Linked List örnekleri
12-
fmt.Println("Linear Linked List Örnekleri:")
11+
fmt.Println("=== Linear Linked List Examples ===")
12+
linearListExamples()
1313

14-
// Integer List
15-
intLinear := linkedlist.NewLinear[int](10)
16-
intLinear.AddToEnd(20)
17-
intLinear.AddToEnd(30)
14+
fmt.Println("\n=== Double Linked List Examples ===")
15+
doubleListExamples()
16+
17+
fmt.Println("\n=== Circular Linked List Examples ===")
18+
circularListExamples()
19+
20+
fmt.Println("\n=== Custom Type Examples ===")
21+
customTypeExamples()
22+
}
23+
24+
func linearListExamples() {
25+
// Create a new Linear List with integers
26+
intList := linkedlist.NewLinear[int](10)
27+
28+
// Define comparison functions
1829
intLess := func(a, b int) bool { return a < b }
19-
intLinear.AddToSequentially(15, intLess)
20-
fmt.Println("\nInteger Linear List:")
21-
intLinear.Print()
30+
intEquals := func(a, b int) bool { return a == b }
31+
32+
// Add elements sequentially
33+
fmt.Println("\nAdding elements sequentially:")
34+
intList.AddToSequentially(5, intLess)
35+
intList.AddToSequentially(15, intLess)
36+
intList.AddToSequentially(12, intLess)
37+
intList.Print() // Expected: 5 10 12 15
2238

23-
// String List
24-
strLinear := linkedlist.NewLinear[string]("Merhaba")
25-
strLinear.AddToEnd("Dünya")
39+
// Add element after a specific value
40+
fmt.Println("\nAdding 13 after 12:")
41+
intList.AddToAfter(13, 12, intEquals)
42+
intList.Print() // Expected: 5 10 12 13 15
43+
44+
// Search for elements
45+
fmt.Printf("\nSearch for 12: %v\n", intList.Search(12, intEquals))
46+
fmt.Printf("Search for 99: %v\n", intList.Search(99, intEquals))
47+
48+
// Delete elements
49+
fmt.Println("\nDeleting 12:")
50+
intList.Delete(12, intEquals)
51+
intList.Print()
52+
53+
// Create a Linear List with strings
54+
strList := linkedlist.NewLinear[string]("Hello")
55+
56+
// Define string comparison functions
2657
strLess := func(a, b string) bool { return strings.Compare(a, b) < 0 }
27-
strLinear.AddToSequentially("Go", strLess)
28-
fmt.Println("\nString Linear List:")
29-
strLinear.Print()
30-
31-
// Double Linked List örnekleri
32-
fmt.Println("\nDouble Linked List Örnekleri:")
33-
34-
// Integer List
35-
intDouble := linkedlist.NewDouble[int](10)
36-
intDouble.AddToEnd(20)
37-
intDouble.AddToEnd(30)
38-
intDouble.AddToSequentially(15, intLess)
39-
fmt.Println("\nInteger Double List (İleri):")
40-
intDouble.Print(false)
41-
fmt.Println("Integer Double List (Geri):")
42-
intDouble.Print(true)
43-
44-
// String List
45-
strDouble := linkedlist.NewDouble[string]("Merhaba")
46-
strDouble.AddToEnd("Dünya")
47-
strDouble.AddToSequentially("Go", strLess)
48-
fmt.Println("\nString Double List (İleri):")
49-
strDouble.Print(false)
50-
fmt.Println("String Double List (Geri):")
51-
strDouble.Print(true)
52-
53-
// Circular Linked List örnekleri
54-
fmt.Println("\nCircular Linked List Örnekleri:")
55-
56-
// Integer List
57-
intCircular := linkedlist.NewCircular[int](10)
58-
intCircular.AddToEnd(20)
59-
intCircular.AddToEnd(30)
60-
intCircular.AddToSequentially(15, intLess)
61-
fmt.Println("\nInteger Circular List:")
62-
intCircular.Print()
63-
64-
// String List
65-
strCircular := linkedlist.NewCircular[string]("Merhaba")
66-
strCircular.AddToEnd("Dünya")
67-
strCircular.AddToSequentially("Go", strLess)
68-
fmt.Println("\nString Circular List:")
69-
strCircular.Print()
70-
71-
// Custom struct örneği
58+
strEquals := func(a, b string) bool { return a == b }
59+
60+
fmt.Println("\nString List Operations:")
61+
strList.AddToSequentially("World", strLess)
62+
strList.AddToSequentially("Go", strLess)
63+
strList.Print() // Expected: Go Hello World
64+
65+
// Search and delete operations with strings
66+
fmt.Printf("Search for 'Hello': %v\n", strList.Search("Hello", strEquals))
67+
strList.Delete("Hello", strEquals)
68+
strList.Print()
69+
}
70+
71+
func doubleListExamples() {
72+
// Create a new Double List with integers
73+
intList := linkedlist.NewDouble[int](10)
74+
75+
// Define comparison functions
76+
intLess := func(a, b int) bool { return a < b }
77+
intEquals := func(a, b int) bool { return a == b }
78+
79+
// Add elements sequentially
80+
fmt.Println("\nAdding elements sequentially:")
81+
intList.AddToSequentially(5, intLess)
82+
intList.AddToSequentially(15, intLess)
83+
intList.AddToSequentially(12, intLess)
84+
85+
// Print forward and backward
86+
fmt.Println("Forward traversal:")
87+
intList.Print(false)
88+
fmt.Println("Backward traversal:")
89+
intList.Print(true)
90+
91+
// Add element after a specific value
92+
fmt.Println("\nAdding 13 after 12:")
93+
intList.AddToAfter(13, 12, intEquals)
94+
intList.Print(false)
95+
96+
// Delete elements
97+
fmt.Println("\nDeleting 12:")
98+
intList.Delete(12, intEquals)
99+
intList.Print(false)
100+
}
101+
102+
func circularListExamples() {
103+
// Create a new Circular List with integers
104+
intList := linkedlist.NewCircular[int](10)
105+
106+
// Define comparison functions
107+
intLess := func(a, b int) bool { return a < b }
108+
intEquals := func(a, b int) bool { return a == b }
109+
110+
// Add elements sequentially
111+
fmt.Println("\nAdding elements sequentially:")
112+
intList.AddToSequentially(5, intLess)
113+
intList.AddToSequentially(15, intLess)
114+
intList.AddToSequentially(12, intLess)
115+
intList.Print()
116+
117+
// Add element after a specific value
118+
fmt.Println("\nAdding 13 after 12:")
119+
intList.AddToAfter(13, 12, intEquals)
120+
intList.Print()
121+
122+
// Delete elements
123+
fmt.Println("\nDeleting 12:")
124+
intList.Delete(12, intEquals)
125+
intList.Print()
126+
}
127+
128+
func customTypeExamples() {
129+
// Define a custom type
72130
type Person struct {
73131
Name string
74132
Age int
75133
}
76134

77-
// Person karşılaştırma fonksiyonları
135+
// Create comparison functions for Person type
78136
personLess := func(a, b Person) bool { return a.Age < b.Age }
137+
personEquals := func(a, b Person) bool {
138+
return a.Name == b.Name && a.Age == b.Age
139+
}
79140

80-
// Linear List with Person
81-
personLinear := linkedlist.NewLinear[Person](Person{Name: "Ali", Age: 25})
82-
personLinear.AddToEnd(Person{Name: "Ayşe", Age: 30})
83-
personLinear.AddToSequentially(Person{Name: "Mehmet", Age: 28}, personLess)
84-
fmt.Println("\nPerson Linear List:")
85-
personLinear.Print()
86-
87-
// Double List with Person
88-
personDouble := linkedlist.NewDouble[Person](Person{Name: "Ali", Age: 25})
89-
personDouble.AddToEnd(Person{Name: "Ayşe", Age: 30})
90-
personDouble.AddToSequentially(Person{Name: "Mehmet", Age: 28}, personLess)
91-
fmt.Println("\nPerson Double List:")
92-
personDouble.Print(false)
93-
94-
// Circular List with Person
95-
personCircular := linkedlist.NewCircular[Person](Person{Name: "Ali", Age: 25})
96-
personCircular.AddToEnd(Person{Name: "Ayşe", Age: 30})
97-
personCircular.AddToSequentially(Person{Name: "Mehmet", Age: 28}, personLess)
98-
fmt.Println("\nPerson Circular List:")
99-
personCircular.Print()
141+
// Create lists of different types with Person
142+
linearList := linkedlist.NewLinear[Person](Person{Name: "Alice", Age: 25})
143+
doubleList := linkedlist.NewDouble[Person](Person{Name: "Bob", Age: 30})
144+
circularList := linkedlist.NewCircular[Person](Person{Name: "Charlie", Age: 35})
145+
146+
// Add elements to Linear List
147+
fmt.Println("\nLinear List with Person type:")
148+
linearList.AddToSequentially(Person{Name: "David", Age: 20}, personLess)
149+
linearList.AddToSequentially(Person{Name: "Eve", Age: 28}, personLess)
150+
linearList.Print()
151+
152+
// Search and delete operations with Person type
153+
searchPerson := Person{Name: "David", Age: 20}
154+
fmt.Printf("Search for %v: %v\n", searchPerson, linearList.Search(searchPerson, personEquals))
155+
linearList.Delete(searchPerson, personEquals)
156+
linearList.Print()
157+
158+
// Add elements to Double List
159+
fmt.Println("\nDouble List with Person type:")
160+
doubleList.AddToSequentially(Person{Name: "Frank", Age: 22}, personLess)
161+
doubleList.AddToSequentially(Person{Name: "Grace", Age: 33}, personLess)
162+
doubleList.Print(false)
163+
fmt.Println("Backward traversal:")
164+
doubleList.Print(true)
165+
166+
// Add elements to Circular List
167+
fmt.Println("\nCircular List with Person type:")
168+
circularList.AddToSequentially(Person{Name: "Henry", Age: 27}, personLess)
169+
circularList.AddToSequentially(Person{Name: "Ivy", Age: 40}, personLess)
170+
circularList.Print()
100171
}

linkedlist/Circular.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,36 @@ func (node *Circular[T]) AddToSequentially(data T, less func(T, T) bool) {
3535
node.mutex.Lock()
3636
defer node.mutex.Unlock()
3737

38-
iter := node
39-
// If the value to be added is less than the value of the root object
40-
if less(data, node.X) {
41-
temp := node.X
42-
node.X = data
43-
newNode := &Circular[T]{X: temp, Next: node.Next}
44-
node.Next = newNode
38+
// Handle the first node
39+
if node.Next == node || less(data, node.X) {
40+
if less(data, node.X) {
41+
temp := node.X
42+
node.X = data
43+
newNode := &Circular[T]{X: temp, Next: node.Next, mutex: sync.RWMutex{}}
44+
node.Next = newNode
45+
} else {
46+
newNode := &Circular[T]{X: data, Next: node, mutex: sync.RWMutex{}}
47+
node.Next = newNode
48+
}
49+
50+
// Update the last node to point to the head
51+
iter := node
4552
for iter.Next != node {
4653
iter = iter.Next
4754
}
4855
iter.Next = node
49-
} else {
50-
// Advance up to the value that is less than the value you want to add.
51-
for iter.Next != node && less(iter.Next.X, data) {
52-
iter = iter.Next
53-
}
54-
// Add the value to the next of the object that is smaller than the value to be added, by creating a new object.
55-
// add the current next to the next of the newly added object
56-
iter.Next = &Circular[T]{X: data, Next: iter.Next}
56+
return
5757
}
58+
59+
// Find the correct position to insert
60+
iter := node
61+
for iter.Next != node && !less(data, iter.Next.X) {
62+
iter = iter.Next
63+
}
64+
65+
// Insert the new node
66+
newNode := &Circular[T]{X: data, Next: iter.Next, mutex: sync.RWMutex{}}
67+
iter.Next = newNode
5868
}
5969

6070
// AddToAfter adds data after the specified value

linkedlist/Circular_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,51 +43,51 @@ func TestCircular_New(t *testing.T) {
4343

4444
func TestCircular_AddToSequentially(t *testing.T) {
4545
tests := []struct {
46-
name string
47-
init int
48-
add []int
49-
want []int
46+
name string
47+
init int
48+
add []int
49+
expected []int
5050
}{
5151
{
52-
name: "add sequentially to empty list",
53-
init: 0,
54-
add: []int{1},
55-
want: []int{0, 1},
52+
name: "add sequentially to empty list",
53+
init: 0,
54+
add: []int{1},
55+
expected: []int{0, 1},
5656
},
5757
{
58-
name: "add sequentially multiple items",
59-
init: 1,
60-
add: []int{2, 3, 4},
61-
want: []int{1, 2, 3, 4},
58+
name: "add sequentially multiple items",
59+
init: 1,
60+
add: []int{2, 3, 4},
61+
expected: []int{1, 2, 3, 4},
6262
},
6363
{
64-
name: "add sequentially with duplicates",
65-
init: 1,
66-
add: []int{2, 2, 3},
67-
want: []int{1, 2, 2, 3},
64+
name: "add sequentially with duplicates",
65+
init: 1,
66+
add: []int{2, 2, 3},
67+
expected: []int{1, 2, 2, 3},
6868
},
6969
{
70-
name: "add sequentially with negative numbers",
71-
init: 0,
72-
add: []int{-2, -1, 1},
73-
want: []int{-2, -1, 0, 1},
70+
name: "add sequentially with negative numbers",
71+
init: 0,
72+
add: []int{-2, -1, 1},
73+
expected: []int{-2, -1, 0, 1},
7474
},
7575
}
7676

7777
for _, tt := range tests {
7878
t.Run(tt.name, func(t *testing.T) {
7979
list := NewCircular(tt.init)
8080
for _, v := range tt.add {
81-
list.AddToSequentially(v, func(a, b int) bool { return a == b })
81+
list.AddToSequentially(v, func(a, b int) bool { return a < b })
8282
}
8383
got := list.List()
84-
if len(got) != len(tt.want) {
85-
t.Errorf("After AddToSequentially() = %v, want %v", got, tt.want)
84+
if len(got) != len(tt.expected) {
85+
t.Errorf("After AddToSequentially() = %v, want %v", got, tt.expected)
8686
return
8787
}
8888
for i, v := range got {
89-
if v != tt.want[i] {
90-
t.Errorf("After AddToSequentially() = %v, want %v", got, tt.want)
89+
if v != tt.expected[i] {
90+
t.Errorf("After AddToSequentially() = %v, want %v", got, tt.expected)
9191
}
9292
}
9393
})

0 commit comments

Comments
 (0)