python/golang实现循环链表的示例代码
作者:NothingLeft了 发布时间:2021-10-31 23:32:20
标签:python,golang,循环链表
循环链表就是将单链表的末尾指向其头部,形成一个环。循环链表的增删操作和单链表的增删操作
区别不大。只是增加时,需要考虑空链表增加第一个节点的特殊情况;删除时需考虑删除节点是头/尾节点,和链表中只有一个节点的特殊情况。
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()
来源:https://www.jianshu.com/p/73095503dfdd
0
投稿
猜你喜欢
- 下面介绍两种查看django 执行的sql语句的方法。方法一:queryset = Apple.objects.all()print que
- 将一个类的接口转换成客户希望的另外一个接口。使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。应用场景:希望复用一些现存的类,但是接
- 一、节点的定义dom节点树图中可见节点HTML文档中的每个成分都是一个节点:整个文档是一个文档节点每个HTML标签是一个元素节点包含在HTM
- 字典dict1 = {'name':'han','age':18,'class
- 进入PyCharm后,点击File→Open,然后在弹窗中选择需要导入项目的文件夹;打开了python项目后,需要配置该项目对应的pytho
- 前言Helium工具是对Selenium的封装,将Selenium工具的使用变得更加简单。Selenium虽然好,但是在它的使用过程中元素的
- 最近想抢冰墩墩的手办和钥匙圈,但是同志们抢的速度太快了,无奈,还是自己写脚本吧。添加火狐浏览器插件Omnibug是一个插件,可以简化web度
- 字符串函数查看字符的ascii码值ascii(str),str是空串时返回0select ascii('a');
- 设计与开发之间本有一线界限,但当时代步入又一个十年,这个线变得更加模糊甚至感觉不到它的存在。使用PS设计网页版面,足矣?或许五年前是吧!现在
- 本文实例为大家分享了JavaScript制作验证码的具体代码,供大家参考,具体内容如下<html><head><
- 当我们拿到一个对象的引用时,如何知道这个对象是什么类型、有哪些方法呢?使用type()首先,我们来判断对象类型,使用type()函数:基本类
- 一、Python 矩阵基本运算引入 numpy 库import numpy as np1. python矩阵操作1)使用
- this指针是面向对象程序设计中的一项重要概念,它表示当前运行的对象。在实现对象的方法时,可以使用this指针来获得该对象自身的引用。和其他
- 在python3.5下安装好matplotlib后,准备显示一张图片测试一下,但是控制台报错说需要安装python3-tk,我天
- 引入依赖#?导入模块import?pymysqlimport?pandas?as?pdimport?numpy?as?npimport?ti
- UUID (Universally Unique Identifier,通用唯一标识)是一个128位的用于计算机系统中以识别信息的数目,虽然
- 在本文中,我挑选了15个最有用的软件包,介绍它们的功能和特点1. DashDash 是比较新的软件包,它是用纯 Python 构建数据可视化
- 传染源: 野生动物,可能为中华菊头蝠病毒: 新型冠状病毒 2019-nCoV传播途径: 经呼吸道飞沫传播,亦可
- 事务日志文件Transaction Log File是用来记录数据库更新情况的文件,扩展名为ldf。在 SQL Server 7.0 和 S
- 最近和Sobin在做一个精品课程的项目,因为用到一个固定的id作为表间关联,所以在前一个表插入数据后要把插入数据生成的自增id传递给下一个表