Python数据结构之单链表
Python数据结构之单链表
单链表有后继结点,无前继结点。
以下实现:
- 创建单链表
- 打印单链表
- 获取单链表的长度
- 判断单链表是否为空
- 在单链表后插入数据
- 获取单链表指定位置的数据
- 获取单链表指定元素的索引
- 删除单链表指定位置的元素
- 更新单链表指定位置的元素
- 清空单链表
class Node(object):
"""定义类来描述指针"""
def __init__(self, data, p=None):
self.data = data
self.next = p
class LinkList(object):
"""单链表"""
def __init__(self):
self.head = None
# 初始化单链表
def create(self, data):
self.head = Node(data[0])
p = self.head
for i in data[1:]:
p.next = Node(i)
p = p.next
# 打印单链表
def print(self):
p = self.head
while p != None:
print(p.data)
p = p.next
# 获取单链表的长度
def len(self):
p = self.head
length = 0
while p != None:
length += 1
p = p.next
return length
# 判断单链表是否为空
def is_empty(self):
return self.len() == 0
# 在单链表后插入数据
def append(self, item):
if self.is_empty():
self.head = Node(item)
else:
p = self.head
while p.next != None:
p = p.next
p.next = Node(item)
# 获取单链表指定位置的数据
def getItem(self, index):
if self.is_empty():
print("单链表为空")
return
if index >= self.len() or index < 0:
print("索引超过单链表长度")
return
p = self.head
count = 0
while count != index:
p = p.next
count += 1
return p.data
# 获取单链表指定元素的索引
def find(self, item):
p = self.head
index = 0
while p != None:
if p.data == item:
return index
p = p.next
index += 1
print("单链表中不存在" + repr(item))
# 在单链表指定位置插入元素
def insert(self, index, item):
if self.is_empty():
print("单链表为空")
return
if index >= self.len() or index < 0:
print("索引超过单链表长度")
return
if index == 0:
self.head = Node(item, self.head)
else:
p = self.head
count = 0
while count < index-1:
p = p.next
count += 1
p.next = Node(item, p.next)
# 删除单链表指定位置的元素
def delete(self, index):
if self.is_empty():
print("单链表为空")
return
if index >= self.len() or index < 0:
print("索引超过单链表长度")
return
if index == 0:
self.head = self.head.next
else:
p = self.head
count = 0
while count < index-1:
p = p.next
count += 1
p.next = p.next.next
# 更新单链表指定位置的元素
def update(self, index, data):
if self.is_empty():
print("单链表为空")
return
if index > self.len() or index < 0:
print("索引超过单链表长度")
return
p = self.head
count = -1
while count < index-1:
p = p.next
count += 1
p.data = data
# 清空单链表
def clear(self):
self.head = None
L = LinkList()
L.create([1, 2, 3])
print("打印单链表:")
L.print()
print("获取单链表的长度:")
print(L.len())
print("单链表是否为空")
print(L.is_empty())
print("在单链表后插入数据")
L.append(4)
L.print()
index = 1
print("获取第" + repr(index) + "个位置的数据")
print(L.getItem(index))
item = 3
print("获取单链表中元素" + repr(item) + "的索引")
print(L.find(item))
index = 2
item = 10
print("在单链表的" + repr(index) + "位置插入数据" + repr(item))
L.insert(index, item)
L.print()
index = 2
print("删除单链表"+repr(index)+"位置的元素")
L.delete(index)
L.print()
index = 2
item = 100
print("更新单链表"+repr(index)+"位置的元素为"+repr(item))
L.update(index, item)
L.print()
print("清空单链表")
L.clear()
L.print()
程序输出结果:
打印单链表:
1
2
3
获取单链表的长度:
3
单链表是否为空
False
在单链表后插入数据
1
2
3
4
获取第1个位置的数据
2
获取单链表中元素3的索引
2
在单链表的2位置插入数据10
1
2
10
3
4
删除单链表2位置的元素
1
2
3
4
更新单链表2位置的元素为100
1
2
100
4
清空单链表
Python数据结构之单链表的更多相关文章
- python 数据结构之单链表的实现
链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...
- python——数据结构之单链表的实现
链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址 信息,所以用一个变量 ...
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- javascript数据结构之单链表
下面是用javascript实现的单链表,但是在输出的时候insert方法中存在问题,chrome的console报错说不能读取空的属性,调试了很久都没有通过,先在这里存着,以后再来修改一下. //数 ...
- 数据结构之单链表的实现-java
一.单链表基本概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元(一般是非连续存储单元)存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素data + 指针next ...
- 【数据结构】单链表介绍及leetcode206题反转单链表python实现
题目传送门:https://leetcode-cn.com/problems/reverse-linked-list/ 文章目录 单链表介绍 链表 概念 种类 优缺点 单链表(slist) leetc ...
- 数据结构:单链表结构字符串(python版)添加了三个新功能
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
- 数据结构:单链表结构字符串(python版)改进
此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
- 数据结构:单链表结构字符串(python版)
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
随机推荐
- TCP协议和UDP协议基础介绍
TCP协议和UDP协议区别 标签(空格分隔): TCP,udp TCP的三次握手 TCP被称为可靠的数据传输协议,主要是通过许多机制来实现的其中最主要的就是三次握手的功能,当然,TCP传送数据的机制非 ...
- c#遍历一个对象中所有的属性和值
SpDictItem sp = GetCFHObject.GetSpItem("); PropertyInfo[] propertys = sp.GetType().GetPropertie ...
- CentOS7 PHP+Redis实现Session共享
先yum简单的安装redis wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/epel-7.repo ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Android辅助开发工具合集
https://github.com/389273716/android-skill-summary/blob/master/开发工具使用指南/辅助开发工具.md
- TZOJ 2569 Wooden Fence(凸包求周长)
描述 Did you ever wonder what happens to your money when you deposit them to a bank account? All banks ...
- redis缓存雪崩、穿透、击穿概念及解决办法
缓存雪崩 对于系统 A,假设每天高峰期每秒 5000 个请求,本来缓存在高峰期可以扛住每秒 4000 个请求,但是缓存机器意外发生了全盘宕机.缓存挂了,此时 1 秒 5000 个请求全部落数据库,数据 ...
- 部署NETCORE在LINUX上报Error -99 EADDRNOTAVAIL address not available
Unable to bind to http://localhost:80 on the IPv6 loopback interface: 'Error -99 EADDRNOTAVAIL addre ...
- SQLServer导入导出命令报错
错误描述: SQL Server阻止了对组件‘xp_cmdshell’的过程‘sys.xp_cmdshell’的访问.因为此组件已作为此服务嚣安全配置的一部分而被关闭. 系统管理员可以通过使用sp_c ...
- vue动态绑定background:url绑不上的问题
场景: 利用swipper做轮播图,在联调的时候发现有些图片存在有些图片不存在 原因:图片路径中存在 (),和 background:url() 会冲突 解决方法: 一:oss图片路径避免出现括号 ( ...