SoFunction
Updated on 2025-03-05

Golang's example code to implement single linked list

1. Define nodes

// Node defines nodestype Node struct {
    Data any
    Next *Node
}

type LinkedList struct {
    headNode *Node // Head pointer}

2. IsEmpty():

// IsEmpty determines whether the linked list is emptyfunc (l *LinkedList) IsEmpty() bool {
    if  == nil {
        return true
    }
    return false
}

3. Length():

// Length Add nodes to the tail of the linked listfunc (l *LinkedList) Length() int {
    currentNode := 
    if currentNode == nil {
        return 0
    }
    length := 0
    for currentNode != nil {
        length++
        currentNode = 
    }
    return length
}

4. AddFromHead():

// AddFromHead Add nodes to the tail of the linked listfunc (l *LinkedList) AddFromHead(data any) {
    node := &Node{data, nil}
    if () { // If the linked list is empty, set the node as the header node         = node
        return
    }
    // Set the head node pointed to by the head pointer to the next of the node and point the head pointer to the node     = 
     = node
}

5. AddFromTail():

// AddFromTail Add nodes to the tail of the linked listfunc (l *LinkedList) AddFromTail(data any) {
    node := &Node{data, nil}
    if () == true {
         = node
        return
    }
    currentNode := 
    for  != nil { // traverse directly to the last node        currentNode = 
    }
     = node
}

6. Insert()

// Insert adds nodes to the specified position in the linked list, and the subscript starts with 0.func (l *LinkedList) Insert(position int, data any) {
    if position &lt;= 0 { // If the position is <=0, add it directly from the head        (data)
    } else if position &gt;= () { // If the position >=() is added directly from the end        (data)
    } else { // Otherwise        node := &amp;Node{data, nil}
        preNode := 
        count := 0
        for count != position-1 { // After the loop exits, pre is just at position-1            preNode = 
            count++
        }
         = 
         = node
    }
}

7. DeleteHead()

// DeleteHead Delete the headerfunc (l *LinkedList) DeleteHead() any {
    if () {
        ("LinkedList is empty")
        return nil
    }
    head := 
     = 
    return head
}

8. DeleteTail()

// DeleteTail Delete the tail nodefunc (l *LinkedList) DeleteTail() any {
    if () {
        ("LinkedList is empty")
        return nil
    }
    currentNode := 
    for  != nil { //If the next node is nil, it means that the next node is the last node        currentNode = 
    }
    data := 
     = nil // Delete the last node    return data
}

9. Remove()

// Remove Delete the specified nodefunc (l *LinkedList) Remove(data any) {
    if () {
        return
    }
    currentNode := 
    if  == data {
         = 
    } else {
        for  != nil { // traverse to the penultimate node            if  == data { // Use the next node to compare (to ensure that the tail node is not missed), the head node has been compared on it                 = 
            } else {
                currentNode = 
            }
        }
    }
}

10. Contain()

// Contain whether the node with a certain value is included in the Contain listfunc (l *LinkedList) Contain(data any) bool {
    if () {
        return false
    }
    currentNode := 
    if  == data {
        return true
    }
    for currentNode != nil {
        if  == data {
            return true
        }
        currentNode = 
    }
    return false
}

11. Traverse()

// Traverse traverse single-linked tablefunc (l *LinkedList) Traverse() {
    if () {
        ("LinkedList is empty")
        return
    }
    currentNode := 
    for currentNode != nil {
        ("%v -&gt; ", )
        currentNode = 
    }
}

This is the end of this article about Golang's example code to implement single-linked lists. For more related Golang's single-linked list content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!