摘要: 本文记录使用go语言对链表的实现。
链表的实现
节点结构
type Node struct {
Data int
Next *Node
}
新建节点
func NewNode(Data int) *Node {
return &Node{
Data: Data,
Next: nil,
}
}
尾部添加节点
// 尾部插入
func Append(head *Node, Data int) *Node {
var cur = head
for cur.Next != nil {
cur = cur.Next
}
var newNode = NewNode(Data)
newNode.Next = cur.Next
cur.Next = newNode
return head
}
头部添加节点
func PreAdd(head *Node, Data int) *Node {
newNode := NewNode(Data)
if head == nil {
return newNode
}
newNode.Next = head
return newNode
}
查看长度
func Length(head *Node) int {
if IsEmpty(head) {
return 0
}
var cur = head
var length int
for cur != nil {
length++
cur = cur.Next
}
return length
}
判断是否为空
func IsEmpty(head *Node) bool {
if head == nil {
return true
}
return false
}
判断是否包含指定值
func Contain(head *Node, Data int) bool {
if IsEmpty(head) {
return false
}
var cur = head
for cur != nil {
if cur.Data == Data {
return true
}
cur = cur.Next
}
return false
}
删除节点
func Delete(head *Node, Data int) *Node {
if head == nil {
return head
}
// 判断是否包含值
if !Contain(head, Data) {![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/ba2d6c3a26bc47f18d82f484f93674a2.png)
return head
}
// 如果头部节点就是要删除的节点
if head.Data == Data {
return head.Next
}
var pre = head
var cur = head.Next
for cur != nil {
if cur.Data == Data {
pre.Next = cur.Next
return head
}
pre = cur
cur = cur.Next
}
return head
}
完整代码
package main
import (
"fmt"
)
/*
1. 新建节点
2. 尾部添加节点
3. 头部添加节点
4. 查看长度
5. 判断是否为空
6. 判断是否包含指定值
7. 删除节点
*/
type Node struct {
Data int
Next *Node
}
func NewNode(Data int) *Node {
return &Node{
Data: Data,
Next: nil,
}
}
func PreAdd(head *Node, Data int) *Node {
newNode := NewNode(Data)
if head == nil {
return newNode
}
newNode.Next = head
return newNode
}
// 尾部插入
func Append(head *Node, Data int) *Node {
var cur = head
for cur.Next != nil {
cur = cur.Next
}
var newNode = NewNode(Data)
newNode.Next = cur.Next
cur.Next = newNode
return head
}
// 遍历链表
func PrintList(head *Node) {
if head == nil {
return
}
cur := head
for cur != nil {
fmt.Printf("%v ", cur.Data)
cur = cur.Next
}
fmt.Println()
}
func IsEmpty(head *Node) bool {
if head == nil {
return true
}
return false
}
func Length(head *Node) int {
if IsEmpty(head) {
return 0
}
var cur = head
var length int
for cur != nil {
length++
cur = cur.Next
}
return length
}
func Contain(head *Node, Data int) bool {
if IsEmpty(head) {
return false
}
var cur = head
for cur != nil {
if cur.Data == Data {
return true
}
cur = cur.Next
}
return false
}
func Delete(head *Node, Data int) *Node {
if head == nil {
return head
}
// 判断是否包含值
if !Contain(head, Data) {
return head
}
// 如果头部节点就是要删除的节点
if head.Data == Data {
return head.Next
}
var pre = head
var cur = head.Next
for cur != nil {
if cur.Data == Data {
pre.Next = cur.Next
return head
}
pre = cur
cur = cur.Next
}
return head
}
func main() {
head := NewNode(1)
head = PreAdd(head, 2)
head = PreAdd(head, 3)
head = PreAdd(head, 4)
head = PreAdd(head, 5)
PrintList(head) // 5 4 3 2 1
length := Length(head)
fmt.Println("length=", length)
fmt.Println(Contain(head, 4))
head2 := NewNode(1)
head2 = Append(head2, 2)
head2 = Append(head2, 3)
head2 = Append(head2, 4)
head2 = Append(head2, 5)
PrintList(head2)
length = Length(head2)
fmt.Println("length=", length)
fmt.Println(Contain(head2, 1))
fmt.Println(Contain(head2, 5))
fmt.Println(Contain(head2, 10))
head2 = Delete(head2, 5)
PrintList(head2)
}