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 相关文章推荐
linux环境下安装pyramid和新建项目的步骤
Nov 27 Python
python多线程threading.Lock锁用法实例
Nov 01 Python
python将图片文件转换成base64编码的方法
Mar 14 Python
Python中使用PyQt把网页转换成PDF操作代码实例
Apr 23 Python
Python装饰器入门学习教程(九步学习)
Jan 28 Python
Python3使用PyQt5制作简单的画板/手写板实例
Oct 19 Python
1 行 Python 代码快速实现 FTP 服务器
Jan 25 Python
python每天定时运行某程序代码
Aug 16 Python
python实现简单俄罗斯方块
Mar 13 Python
pygame用blit()实现动画效果的示例代码
May 28 Python
解决TensorFlow训练模型及保存数量限制的问题
Mar 03 Python
python实战之用emoji表情生成文字
May 08 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合并数组+号和array_merge的区别
2015/06/25 PHP
PHP处理会话函数大总结
2015/08/05 PHP
PHP Echo字符串的连接格式
2016/03/07 PHP
php图片添加文字水印实现代码
2016/03/15 PHP
javascript textContent与innerText的异同分析
2010/10/22 Javascript
JavaScript性能陷阱小结(附实例说明)
2010/12/28 Javascript
jQuery Validate初步体验(二)
2015/12/12 Javascript
JS中的hasOwnProperty()和isPrototypeOf()属性实例详解
2016/08/11 Javascript
轻松掌握JavaScript单例模式
2016/08/25 Javascript
JS正则匹配URL网址的方法(可匹配www,http开头的一切网址)
2017/01/06 Javascript
JavaScript实现鼠标点击导航栏变色特效
2017/02/08 Javascript
Bootstrap显示与隐藏简单实现代码
2017/03/06 Javascript
Mongoose实现虚拟字段查询的方法详解
2017/08/15 Javascript
Vue实现点击后文字变色切换方法
2018/02/11 Javascript
Vue2.5通过json文件读取数据的方法
2018/02/27 Javascript
JS实现的汉字与Unicode码相互转化功能分析
2018/05/25 Javascript
微信小程序实现下拉刷新动画
2019/06/21 Javascript
Node.js学习教程之Module模块
2019/09/03 Javascript
如何阻止小程序遮罩层下方图层滚动
2019/09/05 Javascript
小程序实现长按保存图片的方法
2019/12/31 Javascript
基于javascript处理二进制图片流过程详解
2020/06/08 Javascript
Python获取某一天是星期几的方法示例
2017/01/17 Python
django缓存配置的几种方法详解
2018/07/16 Python
python实现反转部分单向链表
2018/09/27 Python
Python3模拟登录操作实例分析
2019/03/12 Python
python-tkinter之按钮的使用,开关方法
2019/06/11 Python
Java面试题:请说出如下代码的输出结果
2013/04/22 面试题
简历自我评价怎么写好呢?
2014/01/04 职场文书
乔迁之喜主持词
2014/03/27 职场文书
建筑安全标语
2014/06/07 职场文书
重阳节演讲稿:尊敬帮助老人 弘扬传统美德
2014/09/25 职场文书
2014小学教师个人工作总结
2014/11/10 职场文书
2016党员学习作风建设心得体会
2016/01/21 职场文书
MySQL 发生同步延迟时Seconds_Behind_Master还为0的原因
2021/06/21 MySQL
Python FuzzyWuzzy实现模糊匹配
2022/04/28 Python
PostgreSQL常用字符串分割函数整理汇总
2022/07/07 PostgreSQL