链表
-
链表分类,单链表,双链表,环链表
-
时间复杂度,修改时间复杂度O(1)
-
查询时间复杂度,O(n)
package main import "fmt" type Node struct { Data int Next *Node Pre *Node } type LinkedList struct { head *Node current *Node tail *Node } func CreateLinkedList(data []int) { link := LinkedList{} var currentNode *Node for i := 0; i < len(data); i++ { currentNode = new(Node) currentNode.Data = data[i] insertNode(&link, currentNode) } ShowLinkedList(link) } /** 插入 */ func insertNode(list *LinkedList, node *Node) { if list.head == nil { //头指针,当前指针,尾指针都指向当前节点 list.head = node list.current = node list.tail = node } else { //把节点串到最后一个节点的下一个节点 list.tail.Next = node //把当前节点的前向指针指向上一个节点 node.Pre = list.tail //尾指针指向新节点 list.tail = node } } /** 展示每个节点数据 */ func ShowLinkedList(link LinkedList) { var currentNode *Node for currentNode = link.head; currentNode != nil; { fmt.Printf("Node:%d\n", currentNode.Data) currentNode = currentNode.Next } } func main() { data := []int{1, 21, 31, 41, 51, 61, 2, 3, 4, 5, 6, 8, 3, 9} CreateLinkedList(data) }