• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

go语言实现单向链表

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

go语言实现单向链表


        使用go语言实现了单向链表的一些基本功能,存储节点数据的类型为interface{},可以存储各种数据类型。(有什么建议或者不懂的地方可以留言)

实现代码如下:
//节点结构体
type Node struct {
	data interface{}
	nextNode *Node
}
//链表结构体
type List struct {
	root *Node
	count int
}
//添加节点
func (node *Node)addNode(newNode *Node){
	if node.nextNode==nil{
		node.nextNode=newNode
	}else{
		node.nextNode.addNode(newNode)
	}
}
func (list *List)add(data interface{}){
	if data==nil{
		return
	}
	var newNode Node
	newNode.data=data
	if list.root==nil{
		list.root=&newNode
	}else{
		list.root.addNode(&newNode)
	}
	list.count++
}
//返回所有节点的内容
func (list *List)toArray() (datas []interface{}){
	if list.root==nil{
		return nil
	}
	node:=list.root
	for{
		datas=append(datas,node.data)
		if node.nextNode!=nil{
			node=node.nextNode
		}else{
			break
		}
	}
	return datas
}
//查询链表中是否包含某数据
func (list *List)contains(data interface{}) (isCon bool){
	if data==nil || list.root==nil{
		isCon=false
	}
	node:=list.root
	if node.data==data{
		isCon=true
	}else {
		for{
			if node.nextNode!=nil{
				if node.nextNode.data==data{
					isCon=true
					break
				} else{
					node=node.nextNode
				}
			}else{
				isCon=false
				break
			}
		}
	}
	return
}
//根据索引查询数据(从1开始算)
func (list *List)get(index int) (data interface{}){
	if index>list.count{
		data=nil
	}else{
		node:=list.root
		for i:=1;i<index;i++{
			node=node.nextNode
		}
		data=node.data
	}
	return
}
//根据索引更新数据(从1开始算)
func (list *List)set(index int,data interface{}){
	if index>list.count{
		return
	}else{
		node:=list.root
		for i:=1;i<index;i++{
			node=node.nextNode
		}
		node.data=data
	}
}
//删除指定位置的节点(从1开始算)
func (list *List)removeByIndex(index int){
	if index>list.count{
		return
	}else {
		preNode:=list.root
		if index==1{
			list.root=preNode.nextNode
			return
		}
		for i:=1;i<index-1;i++{
			preNode=preNode.nextNode
		}
		preNode.nextNode=preNode.nextNode.nextNode
	}
}
//删除第一次出现的某个值
func (List *List)removeByData(data interface{}){
	if !List.contains(data){
		return
	}
	foot:=1
	node:=List.root
	for{
		if node.data==data{
			List.removeByIndex(foot)
			return
		}else{
			foot++
			node=node.nextNode
		}
	}
}
//查询链表是否为空
func (list *List)isEmpty() bool{
	return list.count==0;
}
//查询链表的长度
func (list *List)size() int{
	return list.count
}
测试代码如下:
func main(){
	var list1 List
	list1.add("hello")
	list1.add(520)
	list1.add('a')
	list1.add(true)
	list1.add(nil)
	fmt.Println(list1.toArray())
	fmt.Println(list1.contains("hello"))
	fmt.Println(list1.get(1))
	list1.set(2,5201314)
	fmt.Println(list1.toArray())
	fmt.Println(list1.isEmpty())
	fmt.Println(list1.size())
	list1.add("baby")
	fmt.Println(list1.toArray())
	list1.removeByIndex(4)
	fmt.Println(list1.toArray())
	list1.removeByData(5201314)
	fmt.Println(list1.toArray())
}
测试结果如下:


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Go语言标准库之time发布时间:2022-07-10
下一篇:
转:区块链开发(一)搭建基于以太坊go-ethereum的私有链环境发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap