gitee.com/quant1x/gox@v1.21.2/util/singlylinkedlist/singlylinkedlist.go (about) 1 // Copyright (c) 2015, Emir Pasic. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package singlylinkedlist implements the singly-linked list. 6 // 7 // Structure is not thread safe. 8 // 9 // Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29 10 package singlylinkedlist 11 12 import ( 13 "fmt" 14 "gitee.com/quant1x/gox/util/internal" 15 "strings" 16 ) 17 18 func assertListImplementation() { 19 var _ internal.List = (*List)(nil) 20 } 21 22 // List holds the elements, where each element points to the next element 23 type List struct { 24 first *element 25 last *element 26 size int 27 } 28 29 type element struct { 30 value interface{} 31 next *element 32 } 33 34 // New instantiates a new list and adds the passed values, if any, to the list 35 func New(values ...interface{}) *List { 36 list := &List{} 37 if len(values) > 0 { 38 list.Add(values...) 39 } 40 return list 41 } 42 43 // Add appends a value (one or more) at the end of the list (same as Append()) 44 func (list *List) Add(values ...interface{}) { 45 for _, value := range values { 46 newElement := &element{value: value} 47 if list.size == 0 { 48 list.first = newElement 49 list.last = newElement 50 } else { 51 list.last.next = newElement 52 list.last = newElement 53 } 54 list.size++ 55 } 56 } 57 58 // Append appends a value (one or more) at the end of the list (same as Add()) 59 func (list *List) Append(values ...interface{}) { 60 list.Add(values...) 61 } 62 63 // Prepend prepends a values (or more) 64 func (list *List) Prepend(values ...interface{}) { 65 // in reverse to keep passed order i.e. ["c","d"] -> Prepend(["a","b"]) -> ["a","b","c",d"] 66 for v := len(values) - 1; v >= 0; v-- { 67 newElement := &element{value: values[v], next: list.first} 68 list.first = newElement 69 if list.size == 0 { 70 list.last = newElement 71 } 72 list.size++ 73 } 74 } 75 76 // Get returns the element at index. 77 // Second return parameter is true if index is within bounds of the array and array is not empty, otherwise false. 78 func (list *List) Get(index int) (interface{}, bool) { 79 80 if !list.withinRange(index) { 81 return nil, false 82 } 83 84 element := list.first 85 for e := 0; e != index; e, element = e+1, element.next { 86 } 87 88 return element.value, true 89 } 90 91 // Remove removes the element at the given index from the list. 92 func (list *List) Remove(index int) { 93 94 if !list.withinRange(index) { 95 return 96 } 97 98 if list.size == 1 { 99 list.Clear() 100 return 101 } 102 103 var beforeElement *element 104 element := list.first 105 for e := 0; e != index; e, element = e+1, element.next { 106 beforeElement = element 107 } 108 109 if element == list.first { 110 list.first = element.next 111 } 112 if element == list.last { 113 list.last = beforeElement 114 } 115 if beforeElement != nil { 116 beforeElement.next = element.next 117 } 118 119 element = nil 120 121 list.size-- 122 } 123 124 // Contains checks if values (one or more) are present in the set. 125 // All values have to be present in the set for the method to return true. 126 // Performance time complexity of n^2. 127 // Returns true if no arguments are passed at all, i.e. set is always super-set of empty set. 128 func (list *List) Contains(values ...interface{}) bool { 129 130 if len(values) == 0 { 131 return true 132 } 133 if list.size == 0 { 134 return false 135 } 136 for _, value := range values { 137 found := false 138 for element := list.first; element != nil; element = element.next { 139 if element.value == value { 140 found = true 141 break 142 } 143 } 144 if !found { 145 return false 146 } 147 } 148 return true 149 } 150 151 // Values returns all elements in the list. 152 func (list *List) Values() []interface{} { 153 values := make([]interface{}, list.size, list.size) 154 for e, element := 0, list.first; element != nil; e, element = e+1, element.next { 155 values[e] = element.value 156 } 157 return values 158 } 159 160 // IndexOf returns index of provided element 161 func (list *List) IndexOf(value interface{}) int { 162 if list.size == 0 { 163 return -1 164 } 165 for index, element := range list.Values() { 166 if element == value { 167 return index 168 } 169 } 170 return -1 171 } 172 173 // Empty returns true if list does not contain any elements. 174 func (list *List) Empty() bool { 175 return list.size == 0 176 } 177 178 // Size returns number of elements within the list. 179 func (list *List) Size() int { 180 return list.size 181 } 182 183 // Clear removes all elements from the list. 184 func (list *List) Clear() { 185 list.size = 0 186 list.first = nil 187 list.last = nil 188 } 189 190 // Sort sort values (in-place) using. 191 func (list *List) Sort(comparator internal.Comparator) { 192 193 if list.size < 2 { 194 return 195 } 196 197 values := list.Values() 198 internal.Sort(values, comparator) 199 200 list.Clear() 201 202 list.Add(values...) 203 204 } 205 206 // Swap swaps values of two elements at the given indices. 207 func (list *List) Swap(i, j int) { 208 if list.withinRange(i) && list.withinRange(j) && i != j { 209 var element1, element2 *element 210 for e, currentElement := 0, list.first; element1 == nil || element2 == nil; e, currentElement = e+1, currentElement.next { 211 switch e { 212 case i: 213 element1 = currentElement 214 case j: 215 element2 = currentElement 216 } 217 } 218 element1.value, element2.value = element2.value, element1.value 219 } 220 } 221 222 // Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right. 223 // Does not do anything if position is negative or bigger than list's size 224 // Note: position equal to list's size is valid, i.e. append. 225 func (list *List) Insert(index int, values ...interface{}) { 226 227 if !list.withinRange(index) { 228 // Append 229 if index == list.size { 230 list.Add(values...) 231 } 232 return 233 } 234 235 list.size += len(values) 236 237 var beforeElement *element 238 foundElement := list.first 239 for e := 0; e != index; e, foundElement = e+1, foundElement.next { 240 beforeElement = foundElement 241 } 242 243 if foundElement == list.first { 244 oldNextElement := list.first 245 for i, value := range values { 246 newElement := &element{value: value} 247 if i == 0 { 248 list.first = newElement 249 } else { 250 beforeElement.next = newElement 251 } 252 beforeElement = newElement 253 } 254 beforeElement.next = oldNextElement 255 } else { 256 oldNextElement := beforeElement.next 257 for _, value := range values { 258 newElement := &element{value: value} 259 beforeElement.next = newElement 260 beforeElement = newElement 261 } 262 beforeElement.next = oldNextElement 263 } 264 } 265 266 // Set value at specified index 267 // Does not do anything if position is negative or bigger than list's size 268 // Note: position equal to list's size is valid, i.e. append. 269 func (list *List) Set(index int, value interface{}) { 270 271 if !list.withinRange(index) { 272 // Append 273 if index == list.size { 274 list.Add(value) 275 } 276 return 277 } 278 279 foundElement := list.first 280 for e := 0; e != index; { 281 e, foundElement = e+1, foundElement.next 282 } 283 foundElement.value = value 284 } 285 286 // String returns a string representation of container 287 func (list *List) String() string { 288 str := "SinglyLinkedList\n" 289 values := []string{} 290 for element := list.first; element != nil; element = element.next { 291 values = append(values, fmt.Sprintf("%v", element.value)) 292 } 293 str += strings.Join(values, ", ") 294 return str 295 } 296 297 // Check that the index is within bounds of the list 298 func (list *List) withinRange(index int) bool { 299 return index >= 0 && index < list.size 300 }