A Swift package providing a lightweight, generic, and singly-linked list data structure with methods for list manipulation, including custom sorting, and easy conversion to arrays and dictionaries.
This package implements a simple, yet flexible LinkedList structure with basic list operations such as insertion, node retrieval, sorting, and conversions. LinkedList and Node structures conform to CustomStringConvertible for easy, readable print output.
To add this package to your project, open your Package.swift file and add the following line to your dependencies:
dependencies: [
.package(url: "https://github.com/RizaMamedow/LinkedListSwift.git", from: "1.0.0")
]
Create an empty list or initialize a list with an array of elements:
var list = LinkedList<Int>()
let listWithValues = LinkedList([1, 2, 3, 4, 5])
- Appending: Add an element to the end of the list.
list.append(10)
- Pushing: Insert an element at the beginning of the list.
list.push(5)
- Get a Node by Index:
let node = list.getNode(at: 2) // retrieves the node at index 2
- Count: Retrieve the count of nodes in the list.
print("Node count: \(list.count)")
- To Array:
let arrayRepresentation = list.toArray()
- To Dictionary:
let dictionaryRepresentation = list.toDictionary()
The list supports custom sorting:
list.sort(by: <) // Sorts in ascending order
Contributions are welcome! Please fork this repository, make your changes, and open a pull request. Be sure to update documentation as needed.
Distributed under the MIT License. See LICENSE
for more information.