Problem: You want to create a linked list data structure.
Solution: Create an element struct that has a pointer to the next element. Wrap another struct around the first element to create a linked list.
A linked list is a linear collection of elements that has each element pointing to the next element. This is different from a list because in lists the elements are next to each other (incrementing the index of the current element will give you access to the next element) while for a linked list this is not necessarily so. As a result, inserting or removing an element is faster because you don’t need to restructure the data structure, unlike in a list. On the other hand, accessing random elements in a linked list is slower because elements are not indexed, so you need to iterate through the elements until the correct one is found.
The functions for the singly linked list are:
Add
• Add a new element to the head of the linked list.
Insert
• Insert a new element into a specific position within the linked list.
Delete
• Remove a specific element from the linked list.
Find
• Find and return an element from the linked list.
In a linked list, you will use two structs — one to represent the data structure, LinkedList , and another to represent an element in the data structure, Element :
import "golang.org/x/exp/constraints" type Element[T constraints.Ordered] struct { value T next *Element[T] } type LinkedList[T constraints.Ordered] struct { head *Element[T] size int }
You need to do this because each element in the linked list points to the next; therefore it needs to keep a pointer to the next. You create an Element struct to represent an element, with a next field pointing to the next element. The Element struct also has a value, which is of type T , constrained by the constraints.Ordered type. This means you can use the ordering operators (<, >, <=, >=) on Element values.
You create a LinkedList struct to keep track of the head of the linked list and its size. You need this struct to associate all the linked list functions.
func (l *LinkedList[T]) Add(el *Element[T]) { if l.head == nil { l.head = el } else { el.next = l.head l.head = el } l.size++ } func (l *LinkedList[T]) Insert(el *Element[T], marker T) error { for current := l.head; current.next != nil; current = current.next { if current.value == marker { el.next = current.next current.next = el // insert el after marker l.size++ return nil } } return errors.New("element not found") } func (l *LinkedList[T]) Delete(el *Element[T]) error { prev := l.head current := l.head for current != nil { if current.value == el.value { if current == l.head { l.head = current.next } else { prev.next = current.next } l.size-- return nil } prev = current current = current.next } return errors.New("element not found") } func (l *LinkedList[T]) Find(value T) (el *Element[T], err error) { for current := l.head; current.next != nil; current = current.next { if current.value == value { el = current break } } if el == nil { err = errors.New("element not found") } return } func (l *LinkedList[T]) List() (list []*Element[T]) { if l.head == nil { return []*Element[T]{} } for current := l.head; current != nil; current = current.next { list = append(list, current) } return } func (l *LinkedList[T]) IsEmpty() bool { return l.size == 0 } func (l *LinkedList[T]) Size() int { return l.size }
The standard library has a container package with a few data structure implementations, and it includes a doubly linked list. If you’re looking for a linked list, this would be a good place to start.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律