create a linked list in swift

To create a linked list in swift, you can use a class to define a node of the list and another class to define the linked list itself.

Here is an example implementation of a linked list in swift:

main.swift
// Node class to represent a single element of the linked list
class Node<T> {
    var value: T
    var next: Node<T>?
    
    init(value: T) {
        self.value = value
    }
}

// Linked list class to hold the elements of the list
class LinkedList<T> {
    var head: Node<T>?
    var tail: Node<T>?
    
    // Add a new value to the end of the list
    func append(value: T) {
        let newNode = Node(value: value)
        
        if let tailNode = tail {
            tailNode.next = newNode
        } else {
            head = newNode
        }
        
        tail = newNode
    }
    
    // Insert a new value at the given index
    func insert(value: T, atIndex index: Int) {
        let newNode = Node(value: value)
        
        if index == 0 {
            newNode.next = head
            head = newNode
            if tail == nil {
                tail = head
            }
            return
        }
        
        var currentNode = head
        for _ in 0..<index-1 {
            currentNode = currentNode?.next
        }
        
        newNode.next = currentNode?.next
        currentNode?.next = newNode
        
        if newNode.next == nil {
            tail = newNode
        }
    }
    
    // Remove the element at the given index
    func remove(atIndex index: Int) -> T? {
        var currentNode = head
        var previousNode: Node<T>?
        
        if index == 0 {
            head = currentNode?.next
            if head == nil {
                tail = nil
            }
            return currentNode?.value
        }
        
        for _ in 0..<index {
            previousNode = currentNode
            currentNode = currentNode?.next
        }
        
        previousNode?.next = currentNode?.next
        
        if previousNode?.next == nil {
            tail = previousNode
        }
        
        return currentNode?.value
    }
    
    // Get the value at a given index
    func value(atIndex index: Int) -> T? {
        var currentNode = head
        
        if index < 0 {
            return nil
        }
        
        for _ in 0..<index {
            currentNode = currentNode?.next
            if currentNode == nil {
                return nil
            }
        }
        
        return currentNode?.value
    }
    
    // Get the index of a given value
    func index(ofValue value: T) -> Int? {
        var currentNode = head
        var currentIndex = 0
        
        while currentNode != nil {
            if currentNode?.value == value {
                return currentIndex
            }
            currentNode = currentNode?.next
            currentIndex += 1
        }
        
        return nil
    }

    // Returns the number of elements in the list
    func count() -> Int {
        var currentNode = head
        var total = 0
        while currentNode != nil {
            total += 1
            currentNode = currentNode?.next
        }
        return total
    }
    
    // Check if the list is empty
    func isEmpty() -> Bool {
        return head == nil
    }
}
3069 chars
132 lines

You can create a new linked list and add elements to it like this:

main.swift
let list = LinkedList<Int>()
list.append(value: 1)
list.append(value: 2)
list.append(value: 3)
95 chars
5 lines

To insert an element at a specific index:

main.swift
list.insert(value: 4, atIndex: 2)
34 chars
2 lines

To remove an element:

main.swift
list.remove(atIndex: 1)
24 chars
2 lines

And to access the values of the list:

main.swift
if let value = list.value(atIndex: 0) {
    print(value)
}
59 chars
4 lines

This will print 1.

gistlibby LogSnag