@@ -8,93 +8,164 @@ import (
88)
99
1010func 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 ("\n Integer Linear List:" )
21- intLinear .Print ()
30+ intEquals := func (a , b int ) bool { return a == b }
31+
32+ // Add elements sequentially
33+ fmt .Println ("\n Adding 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 ("\n Adding 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 ("\n Search 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 ("\n Deleting 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 ("\n String Linear List:" )
29- strLinear .Print ()
30-
31- // Double Linked List örnekleri
32- fmt .Println ("\n Double 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 ("\n Integer 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 ("\n String 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 ("\n Circular 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 ("\n Integer 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 ("\n String 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 ("\n String 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 ("\n Adding 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 ("\n Adding 13 after 12:" )
93+ intList .AddToAfter (13 , 12 , intEquals )
94+ intList .Print (false )
95+
96+ // Delete elements
97+ fmt .Println ("\n Deleting 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 ("\n Adding 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 ("\n Adding 13 after 12:" )
119+ intList .AddToAfter (13 , 12 , intEquals )
120+ intList .Print ()
121+
122+ // Delete elements
123+ fmt .Println ("\n Deleting 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 ("\n Person 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 ("\n Person 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 ("\n Person 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 ("\n Linear 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 ("\n Double 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 ("\n Circular 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}
0 commit comments