1. package main
  2.  
  3. import "fmt"
  4.  
  5. type Object interface{}
  6.  
  7. type Node struct {
  8. data Object
  9. next *Node
  10. }
  11.  
  12. type List struct {
  13. headNode *Node
  14. }
  15.  
  16. func NewNode(data Object, next *Node) *Node {
  17. return &Node{data, next}
  18. }
  19.  
  20. func (list *List) IsEmpty() bool {
  21. return list.headNode == nil
  22. }
  23.  
  24. func (list *List) Add(node *Node) *List {
  25. headNode := list.headNode
  26. if headNode.next != nil {
  27. node.next = headNode.next
  28. }
  29. headNode.next = node
  30. return list
  31. }
  32.  
  33. func (list *List) Append(node *Node) *List {
  34. if list.IsEmpty() {
  35. list.headNode = node
  36. return list
  37. }
  38. curNode := list.headNode
  39. for curNode.next != nil {
  40. curNode = curNode.next
  41. }
  42. curNode.next = node
  43. return list
  44. }
  45.  
  46. func (list *List) Insert(index int, data Object) {
  47. if (index >= && index < list.Length()) {
  48. count :=
  49. if !list.IsEmpty() {
  50. curNode := list.headNode
  51. for curNode != nil && count < index {
  52. count++
  53. curNode = curNode.next
  54. }
  55. node := NewNode(data, curNode.next)
  56. curNode.next = node
  57. }
  58. }
  59. }
  60.  
  61. func (list *List) Remove(index int) {
  62. if (index >= && index < list.Length()) {
  63. count :=
  64. if !list.IsEmpty() {
  65. curNode := list.headNode
  66. for curNode != nil && count < index- {
  67. count++
  68. curNode = curNode.next
  69. }
  70. curNode.next = curNode.next.next
  71. }
  72. }
  73. }
  74.  
  75. func PrintList(list *List) {
  76. cur := list.headNode
  77. for cur != nil {
  78. fmt.Println(cur.data)
  79. cur = cur.next
  80. }
  81. }
  82.  
  83. func (list *List) Length() int {
  84. var length int
  85. curNode := list.headNode
  86. for curNode != nil {
  87. length++
  88. curNode = curNode.next
  89. }
  90. return length
  91. }
  92.  
  93. func ReverseList(head *Node) *Node {
  94. cur := head
  95. var pre *Node = nil
  96. for cur != nil {
  97. pre, cur, cur.next = cur, cur.next, pre
  98. }
  99. return cur
  100. }
  101.  
  102. func main(){
  103. fmt.Println("NewNode======================")
  104. node := NewNode(, nil)
  105. fmt.Println(node.data)
  106. list := &List{node}
  107. PrintList(list)
  108. node2 := NewNode("a", nil)
  109. list.Append(node2)
  110. fmt.Println("Add======================")
  111. PrintList(list)
  112. node1 := NewNode(, nil)
  113. list.Add(node1)
  114. fmt.Println("Length======================")
  115. PrintList(list)
  116. fmt.Println(list.Length())
  117. fmt.Println("Insert======================")
  118. list.Insert(, )
  119. PrintList(list)
  120. fmt.Println("Remove======================")
  121. list.Remove()
  122. PrintList(list)
  123. fmt.Println("ReverseList======================")
  124. ReverseList(node)
  125. PrintList(list)
  126. }

golang实现单链表的更多相关文章

  1. golang构造单链表

    原文地址:http://www.niu12.com/article/47package main import "fmt" type ListNode struct { Value ...

  2. golang数据结构之单链表

    实现单链表的增删查改. 目录如下: singleLink.go package link import ( "fmt" ) //HeroNode 链表节点 type HeroNod ...

  3. 数据结构之单链表(golang版)

    线性表之单链表 package main //线性表中的链式存储结构 //第一个节点为头节点,并不真实保存数据,头节点基本代表了整个链表 import ( "fmt" ) type ...

  4. 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法

    有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...

  5. 单链表的C++实现(采用模板类)

    采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作.  链表结构定义 定义单链表 ...

  6. Java实现单链表的各种操作

    Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素   4.实现链表的反转   5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...

  7. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. c++单链表基本功能

    head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ...

  9. 单链表、循环链表的JS实现

    数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑......   当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...

随机推荐

  1. 微信小程序IOS真机调试发生了SSL 错误,无法建立与该服务器的安全连接

    小程序 真机调试 IOS request:fail 发生了SSL 错误,无法建立与该服务器的安全连接,解决方法服务器中打开Powerhell,执行以下代码,然后重启服务器 # Enables TLS ...

  2. mybatis-generator-plugin

    1.背景 这篇文章刚开始想着哪个分类呢?mybatis.idea或是maven呢,最后还是选择了mybatis.最初使用这个逆向工具是在eclipse上,使用的是eclispe上mbg插件执行配置ge ...

  3. 数据库连接需要dll

    连接oracle引用: Oracle.ManagedDataAccess.dll和Oracle.ManagedDataAccess.EntityFramework.dll, 连接sqlserver 连 ...

  4. 《YouTube 网站的架构演进》阅读笔记

    概述 YouTube 在国内是个404网站,需要翻墙得见,这是有用的废话,先铺垫一下. 从全球网站来看,它仅次于母公司 Google,全球排名位列第2.每天超过5亿以上视频播放量,平均每个用户点击10 ...

  5. redis学习(五)

    一.Redis 发布订阅 1.Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 2.Redis 客户端可以订阅任意数量的频道. 比如你订阅 ...

  6. Golang的运算符-逻辑运算符

    Golang的运算符-逻辑运算符 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.逻辑运算符概述 !: 非运算符,表示NOT(有种取反的意思),如"!ture" ...

  7. HDU 5504:GT and sequence

    GT and sequence  Accepts: 95  Submissions: 1467  Time Limit: 2000/1000 MS (Java/Others)  Memory Limi ...

  8. TensorFlow--交互式使用--tf.InteractiveSession()

    用tf.Session()创建会话时只有在会话中run某个张量才能得到这个张量的运算结果,而交互式环境下如命令行.IPython,想要执行一行就得到结果,这就需要用到tf.InteractiveSes ...

  9. Listener(Web监听器、活化、钝化)

    Web监听器 总共有8个 划分成三种类型 定义一个类,实现接口 注册 | 配置监听器 监听三个作用域创建和销毁 request -httpServletRequest session -httpSes ...

  10. 【转】JS字符(字母)与ASCII码转换方法

    var strVariable; for(var i=0;i<25;i++) { console.log(String.fromCharCode((65+i))); } strVariable. ...