python/golang实现循环链表的示例代码


Posted in Python onSeptember 14, 2020

循环链表就是将单链表的末尾指向其头部,形成一个环。循环链表的增删操作和单链表的增删操作
区别不大。只是增加时,需要考虑空链表增加第一个节点的特殊情况;删除时需考虑删除节点是头/尾节点,和链表中只有一个节点的特殊情况。

golang实现:

type Node struct {
 value int
 next *Node
}

type Circle struct {
 tail *Node
 lenth int
}

// 增加节点:
func (c *Circle) add(value int) {
 newNode := &Node{value, nil}
 if c.lenth == 0 { //空链表中添加节点
 c.tail = newNode
 c.tail.next = newNode
 } else {
 newNode.next = c.tail.next
 c.tail.next = newNode
 c.tail = newNode
 }
 c.lenth += 1
 c.printCircle()
}

// 删除节点:
func (c *Circle) remove(v int) {
 if c.lenth == 0 {
 fmt.Println("空环")
 return
 } else if c.lenth == 1 && c.tail.value == v { //链表中只有一个节点的特殊情况
 c.tail = nil
 c.lenth = 0
 c.printCircle()
 return
 }
 pre := c.tail
 cur := c.tail.next // 头节点
 for i := 0; i < c.lenth; i++ {
 if cur.value == v {
  if cur == c.tail { //如果删除的节点是尾节点,需更新tail
  c.tail = pre
  }
  pre.next = cur.next
  c.lenth -= 1
  c.printCircle()
  return
 }
 pre = cur
 cur = cur.next
 }
 fmt.Println(v, "不在环中")
}

//打印节点:
func (c *Circle) printCircle() {
 if c.lenth == 0 {
 fmt.Println("空环")
 return
 }
 cur := c.tail.next // 头节点
 for i := 0; i < c.lenth; i++ {
 fmt.Printf("%d ", cur.value)
 cur = cur.next
 }
 fmt.Println()
}

func testCircle() {
 var circle *Circle = new(Circle)
 //for i := 1; i <=41; i++ {
 // circle.add(i)
 //}
 circle.add(1)
 circle.remove(10)
 circle.printCircle()
}

python实现:

class Node:
 def __init__(self, value, next=None):
 self.value = value
 self.next = next

 def __str__(self):
 return str(self.value)

class Circle:
 def __init__(self):
 self.tail = None
 self.lenth = 0

 # 增加节点
 def add(self, v):
 new_node = Node(v)
 if self.lenth == 0: # 空链表中添加节点
  self.tail = new_node
  self.tail.next = new_node
 else:
  new_node.next = self.tail.next
  self.tail.next = new_node
  self.tail = new_node
 self.lenth += 1

 # 删除节点
 def remove(self, v):
 if self.lenth == 0:
  print("空环")
  return
 elif self.lenth == 1 and self.tail.value == v: # 链表中只有一个节点的特殊情况
  self.tail = None
  self.lenth = 0
  return
 pre = self.tail
 cur = self.tail.next # 头节点
 for i in range(self.lenth):
  if cur.value == v:
  if cur == self.tail: # 如果删除的节点是尾节点,需更新tail
   self.tail = pre
  pre.next = cur.next
  self.lenth -= 1
  return
  pre = cur
  cur = cur.next
 print(v, "不在环中")

 # 打印链表
 def print_circle(self):
 if self.lenth == 0:
  print('空环')
  return
 cur = self.tail.next # 头节点
 for i in range(self.lenth):
  print(cur, end=" ")
  cur = cur.next
 print()


def test():
 c = Circle()
 for i in range(10):
 c.add(i)
 c.print_circle()
 c.remove(0)
 c.print_circle()
 c.remove(10)
 c.print_circle()
 c.remove(9)
 c.print_circle()
 c.remove(4)
 c.print_circle()

以上就是python/golang实现循环链表的示例代码的详细内容,更多关于python/golang 循环链表的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python 快速排序代码
Nov 23 Python
Python写的创建文件夹自定义函数mkdir()
Aug 25 Python
Python中super关键字用法实例分析
May 28 Python
python入门基础之用户输入与模块初认识
Nov 14 Python
深入探究Django中的Session与Cookie
Jul 30 Python
使用django-crontab实现定时任务的示例
Feb 26 Python
Python+request+unittest实现接口测试框架集成实例
Mar 16 Python
python+numpy+matplotalib实现梯度下降法
Aug 31 Python
Python后台开发Django的教程详解(启动)
Apr 08 Python
pytorch自定义初始化权重的方法
Aug 17 Python
Django将默认的SQLite更换为MySQL的实现
Nov 18 Python
一起来学习Python的元组和列表
Mar 13 Python
python实现canny边缘检测
Sep 14 #Python
Python gevent协程切换实现详解
Sep 14 #Python
通过实例了解python__slots__使用方法
Sep 14 #Python
python如何遍历指定路径下所有文件(按按照时间区间检索)
Sep 14 #Python
详解python实现可视化的MD5、sha256哈希加密小工具
Sep 14 #Python
Python利用pip安装tar.gz格式的离线资源包
Sep 14 #Python
Python tkinter制作单机五子棋游戏
Sep 14 #Python
You might like
PHP+SQL 注入攻击的技术实现以及预防办法
2011/01/27 PHP
ThinkPHP中__initialize()和类的构造函数__construct()用法分析
2014/11/29 PHP
PHP中spl_autoload_register()函数用法实例详解
2016/07/18 PHP
Laravel框架实现利用监听器进行sql语句记录功能
2018/06/06 PHP
getElementByIdx_x js自定义getElementById函数
2012/01/24 Javascript
jquerymobile checkbox及时刷新才能获取其准确值
2012/04/14 Javascript
js解析与序列化json数据(三)json的解析探讨
2013/02/01 Javascript
js中window.open()的所有参数详细解析
2014/01/09 Javascript
经过绑定元素时会多次触发mouseover和mouseout事件
2014/02/28 Javascript
php,js,css字符串截取的办法集锦
2014/09/26 Javascript
JavaScript实现更改网页背景与字体颜色的方法
2015/02/02 Javascript
JavaScript实现的简单幂函数实例
2015/04/17 Javascript
javascript关于open.window子页面执行完成后刷新父页面的问题分析
2015/04/27 Javascript
VUEJS实战之构建基础并渲染出列表(1)
2016/06/13 Javascript
解决webpack打包速度慢的解决办法汇总
2017/07/06 Javascript
Node.js学习之地址解析模块URL的使用详解
2017/09/28 Javascript
详解如何让Express支持async/await
2017/10/09 Javascript
js replace 全局替换的操作方法
2018/06/12 Javascript
javascript将非数值转换为数值
2018/09/13 Javascript
js实现移动端轮播图
2020/12/21 Javascript
详解webpack4.x之搭建前端开发环境
2019/03/28 Javascript
Vue使用Clipboard.JS在h5页面中复制内容实例详解
2019/09/03 Javascript
Vue项目中使用mock.js的完整步骤
2021/01/12 Vue.js
python正则表达式抓取成语网站
2013/11/20 Python
python中Flask框架简单入门实例
2015/03/21 Python
python itchat给指定联系人发消息的方法
2019/06/11 Python
使用Fabric自动化部署Django项目的实现
2019/09/27 Python
Python插件机制实现详解
2020/05/04 Python
如何在python中判断变量的类型
2020/07/29 Python
夜大毕业生自我评价分享
2013/11/10 职场文书
2014年消防工作实施方案
2014/02/20 职场文书
舞蹈毕业生的自我评价
2014/03/05 职场文书
营销学习心得体会
2014/09/12 职场文书
教师作风整改措施思想汇报
2014/10/12 职场文书
2014年惩防体系建设工作总结
2014/12/01 职场文书
《兰兰过桥》教学反思
2016/02/20 职场文书