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 struct模块解析
Jun 12 Python
python实现在pickling的时候压缩的方法
Sep 25 Python
深入浅析python中的多进程、多线程、协程
Jun 22 Python
基于python(urlparse)模板的使用方法总结
Oct 13 Python
python selenium 对浏览器标签页进行关闭和切换的方法
May 21 Python
win7下python3.6安装配置方法图文教程
Jul 31 Python
5款Python程序员高频使用开发工具推荐
Apr 10 Python
详解在Python中以绝对路径或者相对路径导入文件的方法
Aug 30 Python
Django Admin中增加导出CSV功能过程解析
Sep 04 Python
利用python Selenium实现自动登陆京东签到领金币功能
Oct 31 Python
Pycharm安装python库的方法
Nov 24 Python
如何通过python检查文件是否被占用
Dec 18 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 APC缓存配置、使用详解
2014/03/06 PHP
浅谈Laravel POST,PUT,PATCH 路由的区别
2019/10/15 PHP
js表数据排序 sort table data
2009/02/18 Javascript
js中通过split函数分割字符串成数组小例子
2013/09/21 Javascript
点击显示指定元素隐藏其他同辈元素的方法
2014/02/19 Javascript
JavaScript中获取高度和宽度函数总结
2014/10/08 Javascript
jQuery实现模拟marquee标签效果
2015/07/14 Javascript
JavaScript的Backbone.js框架入门学习指引
2016/05/07 Javascript
Vue.js 2.0 移动端拍照压缩图片预览及上传实例
2017/04/27 Javascript
BootStrap 导航条实例代码
2017/05/18 Javascript
基于vue展开收起动画的示例代码
2018/07/05 Javascript
在vue项目中使用md5加密的方法
2018/09/14 Javascript
深入理解 Koa 框架中间件原理
2018/10/18 Javascript
深入浅析Vue.js 中的 v-for 列表渲染指令
2018/11/19 Javascript
微信小程序MUI导航栏透明渐变功能示例(通过改变rgba的a值实现)
2019/01/24 Javascript
关于微信小程序获取小程序码并接受buffer流保存为图片的方法
2019/06/07 Javascript
如何在微信小程序中实现Mixins方案
2019/06/20 Javascript
只有 20 行的 JavaScript 模板引擎实例详解
2020/05/11 Javascript
python实现探测socket和web服务示例
2014/03/28 Python
Python hexstring-list-str之间的转换方法
2019/06/12 Python
python的一些加密方法及python 加密模块
2019/07/11 Python
python实现几种归一化方法(Normalization Method)
2019/07/31 Python
PyTorch加载预训练模型实例(pretrained)
2020/01/17 Python
服务器端jupyter notebook映射到本地浏览器的操作
2020/04/14 Python
华为的Java面试题
2014/03/07 面试题
C语言开发工程师测试题
2016/12/20 面试题
Linux如何命名文件--使用文件名时应注意
2014/05/29 面试题
华为python面试题
2016/05/03 面试题
群众路线剖析材料
2014/02/02 职场文书
逃课上网检讨书
2014/02/20 职场文书
ktv好的活动方案
2014/08/15 职场文书
2015年社区计生工作总结
2015/04/21 职场文书
叶问观后感
2015/06/15 职场文书
毕业典礼致辞
2015/07/29 职场文书
Flask搭建一个API服务器的步骤
2021/05/28 Python
Linux磁盘管理方法介绍
2022/06/01 Servers