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求两个文本文件以行为单位的交集、并集与差集的方法
Jun 17 Python
详解使用python的logging模块在stdout输出的两种方法
May 17 Python
python对视频画框标记后保存的方法
Dec 07 Python
python使用adbapi实现MySQL数据库的异步存储
Mar 19 Python
使用Python制作简单的小程序IP查看器功能
Apr 16 Python
Django对数据库进行添加与更新的例子
Jul 12 Python
Python解压 rar、zip、tar文件的方法
Nov 19 Python
Python远程开发环境部署与调试过程图解
Dec 09 Python
解决springboot yml配置 logging.level 报错问题
Feb 21 Python
Python调用.net动态库实现过程解析
Jun 05 Python
python识别验证码的思路及解决方案
Sep 13 Python
Python之京东商品秒杀的实现示例
Jan 06 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操作数组的一些函数整理介绍
2011/07/17 PHP
php数组一对一替换实现代码
2012/08/31 PHP
php基于curl扩展制作跨平台的restfule 接口
2015/05/11 PHP
php文件读取方法实例分析
2015/06/20 PHP
详解PHP中foreach的用法和实例
2016/10/25 PHP
php实现的rc4加密解密类定义与用法示例
2018/08/16 PHP
Yii2 queue的队列使用详解
2019/07/19 PHP
html下载本地
2006/06/19 Javascript
js变量作用域及可访问性的探讨
2006/11/23 Javascript
IE6/7 and IE8/9/10(IE7模式)依次隐藏具有absolute或relative的父元素和子元素后再显示父元素
2011/07/31 Javascript
js模拟hashtable的简单实例
2014/03/06 Javascript
js+canvas绘制矩形的方法
2016/01/28 Javascript
JavaScript实现九九乘法表的简单实例
2016/06/07 Javascript
AngularJS 使用ng-repeat报错 [ngRepeat:dupes]
2017/01/19 Javascript
浅谈js停止事件冒泡 阻止浏览器的默认行为(阻止超连接 #)
2017/02/08 Javascript
vue.js利用Object.defineProperty实现双向绑定
2017/03/09 Javascript
通过js随机函数Math.random实现乱序
2020/05/19 Javascript
js利用iframe实现选项卡效果
2020/08/09 Javascript
微信小程序入门之指南针
2020/10/22 Javascript
[43:57]Liquid vs Mineski 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
Python字符和字符值(ASCII或Unicode码值)转换方法
2015/05/21 Python
使用Python解析JSON数据的基本方法
2015/10/15 Python
python运行其他程序的实现方法
2017/07/14 Python
python入门教程 python入门神图一张
2018/03/05 Python
Python3使用pandas模块读写excel操作示例
2018/07/03 Python
python实现图片插入文字
2019/11/26 Python
python3.6连接mysql数据库及增删改查操作详解
2020/02/10 Python
python函数map()和partial()的知识点总结
2020/05/26 Python
python pygame 愤怒的小鸟游戏示例代码
2021/02/25 Python
捷克电器和DJ设备网上商店:Electronic-star
2017/07/18 全球购物
实现strstr功能,即在父串中寻找子串首次出现的位置
2016/08/05 面试题
新闻网站实习自我鉴定
2013/09/25 职场文书
2014党支部对照检查材料思想汇报
2014/10/05 职场文书
教师学期个人总结
2015/02/11 职场文书
详解Spring Security中的HttpBasic登录验证模式
2022/03/17 Java/Android
python pandas 解析(读取、写入)CSV 文件的操作方法
2022/12/24 Python