golang实现单链表
- package main
- import "fmt"
- type Object interface{}
- type Node struct {
- data Object
- next *Node
- }
- type List struct {
- headNode *Node
- }
- func NewNode(data Object, next *Node) *Node {
- return &Node{data, next}
- }
- func (list *List) IsEmpty() bool {
- return list.headNode == nil
- }
- func (list *List) Add(node *Node) *List {
- headNode := list.headNode
- if headNode.next != nil {
- node.next = headNode.next
- }
- headNode.next = node
- return list
- }
- func (list *List) Append(node *Node) *List {
- if list.IsEmpty() {
- list.headNode = node
- return list
- }
- curNode := list.headNode
- for curNode.next != nil {
- curNode = curNode.next
- }
- curNode.next = node
- return list
- }
- func (list *List) Insert(index int, data Object) {
- if (index >= && index < list.Length()) {
- count :=
- if !list.IsEmpty() {
- curNode := list.headNode
- for curNode != nil && count < index {
- count++
- curNode = curNode.next
- }
- node := NewNode(data, curNode.next)
- curNode.next = node
- }
- }
- }
- func (list *List) Remove(index int) {
- if (index >= && index < list.Length()) {
- count :=
- if !list.IsEmpty() {
- curNode := list.headNode
- for curNode != nil && count < index- {
- count++
- curNode = curNode.next
- }
- curNode.next = curNode.next.next
- }
- }
- }
- func PrintList(list *List) {
- cur := list.headNode
- for cur != nil {
- fmt.Println(cur.data)
- cur = cur.next
- }
- }
- func (list *List) Length() int {
- var length int
- curNode := list.headNode
- for curNode != nil {
- length++
- curNode = curNode.next
- }
- return length
- }
- func ReverseList(head *Node) *Node {
- cur := head
- var pre *Node = nil
- for cur != nil {
- pre, cur, cur.next = cur, cur.next, pre
- }
- return cur
- }
- func main(){
- fmt.Println("NewNode======================")
- node := NewNode(, nil)
- fmt.Println(node.data)
- list := &List{node}
- PrintList(list)
- node2 := NewNode("a", nil)
- list.Append(node2)
- fmt.Println("Add======================")
- PrintList(list)
- node1 := NewNode(, nil)
- list.Add(node1)
- fmt.Println("Length======================")
- PrintList(list)
- fmt.Println(list.Length())
- fmt.Println("Insert======================")
- list.Insert(, )
- PrintList(list)
- fmt.Println("Remove======================")
- list.Remove()
- PrintList(list)
- fmt.Println("ReverseList======================")
- ReverseList(node)
- PrintList(list)
- }
golang实现单链表的更多相关文章
- golang构造单链表
原文地址:http://www.niu12.com/article/47package main import "fmt" type ListNode struct { Value ...
- golang数据结构之单链表
实现单链表的增删查改. 目录如下: singleLink.go package link import ( "fmt" ) //HeroNode 链表节点 type HeroNod ...
- 数据结构之单链表(golang版)
线性表之单链表 package main //线性表中的链式存储结构 //第一个节点为头节点,并不真实保存数据,头节点基本代表了整个链表 import ( "fmt" ) type ...
- 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法
有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...
- 单链表的C++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
- Java实现单链表的各种操作
Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素 4.实现链表的反转 5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- c++单链表基本功能
head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ...
- 单链表、循环链表的JS实现
数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑...... 当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...
随机推荐
- 微信小程序IOS真机调试发生了SSL 错误,无法建立与该服务器的安全连接
小程序 真机调试 IOS request:fail 发生了SSL 错误,无法建立与该服务器的安全连接,解决方法服务器中打开Powerhell,执行以下代码,然后重启服务器 # Enables TLS ...
- mybatis-generator-plugin
1.背景 这篇文章刚开始想着哪个分类呢?mybatis.idea或是maven呢,最后还是选择了mybatis.最初使用这个逆向工具是在eclipse上,使用的是eclispe上mbg插件执行配置ge ...
- 数据库连接需要dll
连接oracle引用: Oracle.ManagedDataAccess.dll和Oracle.ManagedDataAccess.EntityFramework.dll, 连接sqlserver 连 ...
- 《YouTube 网站的架构演进》阅读笔记
概述 YouTube 在国内是个404网站,需要翻墙得见,这是有用的废话,先铺垫一下. 从全球网站来看,它仅次于母公司 Google,全球排名位列第2.每天超过5亿以上视频播放量,平均每个用户点击10 ...
- redis学习(五)
一.Redis 发布订阅 1.Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 2.Redis 客户端可以订阅任意数量的频道. 比如你订阅 ...
- Golang的运算符-逻辑运算符
Golang的运算符-逻辑运算符 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.逻辑运算符概述 !: 非运算符,表示NOT(有种取反的意思),如"!ture" ...
- HDU 5504:GT and sequence
GT and sequence Accepts: 95 Submissions: 1467 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- TensorFlow--交互式使用--tf.InteractiveSession()
用tf.Session()创建会话时只有在会话中run某个张量才能得到这个张量的运算结果,而交互式环境下如命令行.IPython,想要执行一行就得到结果,这就需要用到tf.InteractiveSes ...
- Listener(Web监听器、活化、钝化)
Web监听器 总共有8个 划分成三种类型 定义一个类,实现接口 注册 | 配置监听器 监听三个作用域创建和销毁 request -httpServletRequest session -httpSes ...
- 【转】JS字符(字母)与ASCII码转换方法
var strVariable; for(var i=0;i<25;i++) { console.log(String.fromCharCode((65+i))); } strVariable. ...