Go-单链表

最简单案例

package main

import "fmt"

type NodeList struct {
	data int
	next *NodeList
}

func ShowNode(node *NodeList) {
	for node != nil {
		//fmt.Printf("type :%T ,value:%v\n",*node,*node)
		//fmt.Printf("data:%v,next:%v--->",node.data,node.next)
		fmt.Printf("--->%v\n", node.data)
		node = node.next //移动指针
	}
}

func InsertHeadData() {
	var head = new(NodeList)
	head.data = 0
	var tail *NodeList
	tail = head
	for i := 1; i < 10; i++ {
		var node = NodeList{data: i}
		node.next = tail
		tail = &node
	}
	ShowNode(tail)
}

func InsertTailData() {
	var head = new(NodeList)
	head.data = 0
	var newList *NodeList
	newList = head
	for i := 1; i < 10; i++ {
		var node = NodeList{data: i}
		(*newList).next = &node
		newList = &node
	}
	ShowNode(head)
}

func main() {

	InsertTailData()
}

单链表-往最后面插入

复制
package main

import "fmt"

type HeroNode struct {
	no       int
	name     string
	nickname string
	next     *HeroNode
}

//在单链表之后加入
func InsertHeroNode(head *HeroNode, newHeroNode *HeroNode) {
	temp := head
	for {
		if temp.next == nil {
			break
		}
		temp = temp.next
	}
	temp.next = newHeroNode
}

//显示链表的所有信息
func ListHeroNode(head *HeroNode) {
	temp := head
	if temp.next == nil {
		fmt.Println("空链表")
		return
	}
	for {
		fmt.Printf("[%d,%s,%s]==>", temp.next.no, temp.next.name, temp.next.nickname)
		temp = temp.next
		if temp.next == nil {
			break
		}
	}
}

func main() {
	head := &HeroNode{} //头结点不需要初始化
	hero1 := &HeroNode{
		no:       1,
		name:     "宋江",
		nickname: "及时雨",
	}
	hero2 := &HeroNode{
		no:       2,
		name:     "卢俊义",
		nickname: "玉麒麟",
	}
	InsertHeroNode(head, hero1)
	InsertHeroNode(head, hero2)
	ListHeroNode(head)

}

单链表的有序插入

复制
package main

import "fmt"

type HeroNode struct {
	no       int
	name     string
	nickname string
	next     *HeroNode
}

//在单链表之后加入
func InsertHeroNode(head *HeroNode, newHeroNode *HeroNode) {
	temp := head
	flag := true
	for {
		if temp.next == nil {
			break
		} else if temp.next.no > newHeroNode.no {
			break
		} else if temp.next.no == newHeroNode.no {
			flag = false
			break
		}
		temp = temp.next
	}
	if !flag {
		fmt.Println("对不起,已经存在no=", newHeroNode.no)
		return
	} else {
		newHeroNode.next = temp.next
		temp.next = newHeroNode
	}
}

//显示链表的所有信息
func ListHeroNode(head *HeroNode) {
	temp := head
	for {
		if temp.next == nil {
			return
		}
		fmt.Printf("[%d,%s,%s]==>", temp.next.no, temp.next.name, temp.next.nickname)
		temp = temp.next
		if temp.next == nil {
			break
		}
	}
}

func main() {
	head := &HeroNode{} //头结点不需要初始化
	hero1 := &HeroNode{
		no:       1,
		name:     "宋江",
		nickname: "及时雨",
	}
	hero2 := &HeroNode{
		no:       2,
		name:     "卢俊义",
		nickname: "玉麒麟",
	}
	hero3 := &HeroNode{
		no:       3,
		name:     "林冲",
		nickname: "豹子头",
	}
	InsertHeroNode(head, hero1)
	InsertHeroNode(head, hero3)
	InsertHeroNode(head, hero2)
	ListHeroNode(head)

}

单链表的有序插入(允许序号重复)

复制
package main

import "fmt"

type HeroNode struct {
	no       int
	name     string
	nickname string
	next     *HeroNode
}

//在单链表之后加入
func InsertHeroNode(head *HeroNode, newHeroNode *HeroNode) {
	temp := head
	for {
		if temp.next == nil {
			break
		} else if temp.next.no > newHeroNode.no {
			break
		}
		temp = temp.next
	}
	newHeroNode.next = temp.next
	temp.next = newHeroNode
}

//显示链表的所有信息
func ListHeroNode(head *HeroNode) {
	temp := head
	for {
		if temp.next == nil {
			return
		}
		fmt.Printf("[%d,%s,%s]==>", temp.next.no, temp.next.name, temp.next.nickname)
		temp = temp.next
		if temp.next == nil {
			break
		}
	}
}

func main() {
	head := &HeroNode{} //头结点不需要初始化
	hero1 := &HeroNode{
		no:       1,
		name:     "宋江",
		nickname: "及时雨",
	}
	hero2 := &HeroNode{
		no:       2,
		name:     "卢俊义",
		nickname: "玉麒麟",
	}
	hero3 := &HeroNode{
		no:       2,
		name:     "林冲",
		nickname: "豹子头",
	}
	InsertHeroNode(head, hero3)
	InsertHeroNode(head, hero2)
	InsertHeroNode(head, hero1)
	ListHeroNode(head)

}

 

posted @ 2022-09-18 02:44  南昌拌粉的成长  阅读(20)  评论(0编辑  收藏  举报