package main
import (
"errors"
"fmt"
)
type Node struct {
key int
value string
next * Node
}
func newList()(head *Node) {
head = &Node {
next: nil,
}
return
}
func addHeadList(head, newNode *Node) {
if head.next == nil {
head.next = newNode
}else {
newNode.next = head.next
head.next = newNode
}
}
func addTailList(head, newNode *Node) {
tmp := head
for ;tmp.next != nil; tmp = tmp.next {
}
tmp.next = newNode
}
func findNodebyKey(head *Node, key int)(keyNode *Node, err error) {
if head.next == nil {
fmt.Println("链表为空")
return
}
flag := false
tmp := head
for tmp.next != nil {
if tmp.next.key == key {
keyNode = tmp.next
flag = true
}
tmp = tmp.next
}
if !flag {
err = errors.New("链表中找不到该key对应的Node")
}
return
}
func findNodeByLocation(head *Node, seat int) (seatNode *Node, err error){
if head.next == nil {
err = errors.New("链表为空")
return
}
if seat < 1 {
err = errors.New("传入的位置有误,请重新操作")
return
}
tmp := head
i := 1
for tmp.next != nil {
if i == seat {
seatNode = tmp.next
return
}
i++
tmp = tmp.next
}
err = errors.New(fmt.Sprintf("链表的长度为:%d,小于你输入的位置:%d",i-1,seat))
return
}
func insertByLocation(head,insertNode *Node,seat int) (err error){
if head.next == nil {
err = errors.New("链表为空")
return
}
if seat <1 {
err = errors.New("传入seat参数有误,请重新操作")
return
}
tmp := head
if seat == 1{
insertNode.next = tmp.next
tmp.next = insertNode
return
}
i :=0
for tmp.next != nil {
if i == seat-1 {
insertNode.next =tmp.next
tmp.next = insertNode
return
}
tmp = tmp.next
i++
}
if i+1 == seat {
tmp.next = insertNode
return
}
err = errors.New(fmt.Sprintf("链表的长度为:%d,你要插入的位置:%d",i,seat))
return
}
func delByLocation(head *Node,seat int) (err error) {
if seat <1 {
err = errors.New("输入的位置有误")
return
}
if head.next == nil {
err = errors.New("链表为空")
return
}
tmp := head
i :=0
for tmp.next != nil {
if i == seat -1 {
tmp.next = tmp.next.next
return
}
i++
fmt.Printf("------i:%d------\n",i)
tmp = tmp.next
}
err = errors.New(fmt.Sprintf(">>>链表的长度为:%d,你要删除的位置为:%d<<<\n",i,seat))
return
}
func addByOrder(head,newNode *Node) {
if head.next == nil {
head.next = newNode
return
}
tmp := head
for tmp.next != nil {
if tmp.next.key > newNode.key {
newNode.next = tmp.next
tmp.next = newNode
return
}
tmp = tmp.next
}
tmp.next = newNode
}
func showList(head *Node) {
if head.next == nil {
fmt.Println("链表为空")
return
}
tmp := head
for tmp.next != nil {
tmp = tmp.next
fmt.Printf("key:%d,value:%s>>>",tmp.key,tmp.value)
}
fmt.Println()
}
func main() {
fmt.Println("come in main()----list")
defer fmt.Println("out of main()---list")
head := newList()
node1 := &Node{
value: "1111",
key:1,
}
node2 := &Node {
value: "2222",
key:2,
}
node3 := &Node{
value: "3333",
key:3,
}
node4 := &Node {
value :"44444",
key:4,
}
node5 := &Node {
value :"5555",
key:5,
}
addTailList(head,node1)
addTailList(head,node2)
addTailList(head,node3)
addByOrder(head,node5)
addByOrder(head,node4)
showList(head)
node , err :=findNodebyKey(head,1)
if err != nil {
fmt.Println("err :",err)
return
}
fmt.Printf("node:%#v\n",node)
seatNode, err := findNodeByLocation(head,5)
if err != nil {
fmt.Println("err :",err)
return
}
fmt.Printf("seatNode:%#v\n",seatNode)
err = delByLocation(head,6)
if err != nil {
fmt.Println(">>>>>>>>>err :",err)
return
}
fmt.Println("------------------------")
showList(head)
}
|