需求
实现循环链表的节点生成、顺序遍历、指定删除。
实现
go
package main
import (
"fmt"
)
type zodiac_sign struct {
number int
dizhi string
animal string
year int
next *zodiac_sign
}
// 添加
// func add_node_by_order(previous_node zodiac_sign, current_node zodiac_sign) zodiac_sign {
func add_node_by_order(previous_node *zodiac_sign, current_node *zodiac_sign) *zodiac_sign {
if (previous_node.number+1 == current_node.number) || (previous_node.number == 12 && current_node.number == 1) {
previous_node.next = current_node
fmt.Printf("节点%s的next指针已指向节点%s的地址%p\n", previous_node.animal, current_node.animal, previous_node.next)
return current_node
} else {
fmt.Printf("顺序错误:禁止添加新节点%s\n", current_node.animal)
return previous_node
}
}
// 查找
func check_node_by_order(start_node *zodiac_sign, current_node *zodiac_sign) {
if current_node.next != start_node {
fmt.Printf("%d年为%s年\n", current_node.year, current_node.animal)
check_node_by_order(start_node, current_node.next)
} else {
fmt.Printf("最后一年为%d年,是%s年\n", current_node.year, current_node.animal)
}
}
// 删除
func delete_node_by_id(current_node *zodiac_sign, id int) {
if id < 1 || id > 12 {
fmt.Print("输入参数不在范围内!\n")
} else {
if (current_node.number + 1) == id {
fmt.Printf("成功找到节点%s\n", current_node.next.animal)
current_node.next = current_node.next.next
fmt.Printf("节点%s的next指针已经指向%s\n", current_node.animal, current_node.next.animal)
} else {
delete_node_by_id(current_node.next, id)
}
}
}
func main() {
mouse := &zodiac_sign{number: 1, dizhi: "子", animal: "鼠", year: 2020}
cow := &zodiac_sign{number: 2, dizhi: "丑", animal: "牛", year: 2021}
tiger := &zodiac_sign{number: 3, dizhi: "寅", animal: "虎", year: 2022}
rabbit := &zodiac_sign{number: 4, dizhi: "卯", animal: "兔", year: 2023}
dragon := &zodiac_sign{number: 5, dizhi: "辰", animal: "龙", year: 2024}
snake := &zodiac_sign{number: 6, dizhi: "巳", animal: "蛇", year: 2025}
horse := &zodiac_sign{number: 7, dizhi: "午", animal: "马", year: 2026}
sheep := &zodiac_sign{number: 8, dizhi: "未", animal: "羊", year: 2027}
monkey := &zodiac_sign{number: 9, dizhi: "申", animal: "猴", year: 2028}
cock := &zodiac_sign{number: 10, dizhi: "酉", animal: "鸡", year: 2029}
dog := &zodiac_sign{number: 11, dizhi: "戌", animal: "狗", year: 2030}
pig := &zodiac_sign{number: 12, dizhi: "亥", animal: "猪", year: 2031}
p := add_node_by_order(mouse, cow)
p = add_node_by_order(p, cow)
p = add_node_by_order(p, tiger)
p = add_node_by_order(p, rabbit)
p = add_node_by_order(p, dragon)
p = add_node_by_order(p, snake)
// 错误连接
add_node_by_order(p, pig)
p = add_node_by_order(p, horse)
p = add_node_by_order(p, sheep)
p = add_node_by_order(p, monkey)
// 错误连接
add_node_by_order(p, rabbit)
p = add_node_by_order(p, cock)
p = add_node_by_order(p, dog)
p = add_node_by_order(p, pig)
p = add_node_by_order(pig, mouse)
check_node_by_order(mouse, mouse)
delete_node_by_id(rabbit, 5)
}
powershell
节点鼠的next指针已指向节点牛的地址0xc000030180
顺序错误:禁止添加新节点牛
节点牛的next指针已指向节点虎的地址0xc0000301c0
节点虎的next指针已指向节点兔的地址0xc000030200
节点兔的next指针已指向节点龙的地址0xc000030240
节点龙的next指针已指向节点蛇的地址0xc000030280
顺序错误:禁止添加新节点猪
节点蛇的next指针已指向节点马的地址0xc0000302c0
节点马的next指针已指向节点羊的地址0xc000030300
节点羊的next指针已指向节点猴的地址0xc000030340
顺序错误:禁止添加新节点兔
节点猴的next指针已指向节点鸡的地址0xc000030380
节点鸡的next指针已指向节点狗的地址0xc0000303c0
节点狗的next指针已指向节点猪的地址0xc000030400
节点猪的next指针已指向节点鼠的地址0xc000030140
2020年为鼠年
2021年为牛年
2022年为虎年
2023年为兔年
2024年为龙年
2025年为蛇年
2026年为马年
2027年为羊年
2028年为猴年
2029年为鸡年
2030年为狗年
最后一年为2031年,是猪年
成功找到节点龙
节点兔的next指针已经指向蛇