Home  >  Article  >  Backend Development  >  How to implement linked list in golang

How to implement linked list in golang

小老鼠
小老鼠Original
2023-12-14 16:37:311127browse

Method to implement linked list: 1. Define a Node structure to represent the nodes of the linked list. Each node contains a data item and a pointer to the next node; 2. Define a LinkedList structure to represent the linked list itself, which contains a pointer to the head node of the linked list; 3. Two methods are implemented, append is used to insert a node at the end of the linked list, and printList is used to print the elements of the linked list; 4. In this way, you can use Go Language structures and pointers are used to implement the basic functions of linked lists.

How to implement linked list in golang

The operating system for this tutorial: Windows 10 system, go1.20.1 version, Dell G3 computer.

In the Go language, structures and pointers can be used to implement linked lists. The following is a simple example that shows how to implement a simple one-way linked list in the Go language:

package main
import "fmt"
// 定义链表节点
type Node struct {
    data int
    next *Node
}
// 定义链表
type LinkedList struct {
    head *Node
}
// 在链表末尾插入节点
func (list *LinkedList) append(data int) {
    newNode := &Node{data: data, next: nil}
    if list.head == nil {
        list.head = newNode
        return
    }
    lastNode := list.head
    for lastNode.next != nil {
        lastNode = lastNode.next
    }
    lastNode.next = newNode
}
// 打印链表元素
func (list *LinkedList) printList() {
    currentNode := list.head
    for currentNode != nil {
        fmt.Printf("%d -> ", currentNode.data)
        currentNode = currentNode.next
    }
    fmt.Println("nil")
}
func main() {
    // 创建链表
    var list LinkedList
    list.append(1)
    list.append(2)
    list.append(3)
    // 打印链表
    list.printList()
}

In the above example, we first define a Node structure to represent the nodes of the linked list. Each node contains a data item and a pointer to the next node. Then, we define a LinkedList structure to represent the linked list itself, which contains a pointer to the head node of the linked list. We implemented two methods, append is used to insert nodes at the end of the linked list, and printList is used to print the elements of the linked list.

In this way, we can use the structures and pointers of the Go language to implement the basic functions of the linked list. Of course, this is just a simple example, and actual linked lists may be more complex, such as doubly linked lists, circular linked lists, etc.

The above is the detailed content of How to implement linked list in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn