数据结构学习--单链表(python)
概念
链表(linked_list)是物理存储单元上非连续的、非顺序的存储结构,数据元素的逻辑顺序
是通过链表的指针地址实现,每个元素包含两个结点,一个是存储元素的数据域 (内存空间)
,另一个是指向下一个结点地址的指针域。根据指针的指向,链表能形成不同的结构,例如
单链表,双向链表,循环链表等.
链表通过将链点 i 与其邻居链点 i+1 通过指针相关联,从索引 0 到索引 N-1 对链点进
行排序.
实现
class Node:
"""
链点
"""
def __init__(self, data):
self.data = data # 数据域
self.next = None # 指针域
class SingleLinkedList:
"""
单链表
"""
def __init__(self, head=None):
self.head = Node(head) # 链表头部元素
self._length = None
# 向链表中添加新链点
def append(self, value):
self._length = None
new_node = Node(value)
# 将头部节点指向临时变量
current = self.head
# 当头部节点存在时
if self.head:
# 循环遍历到链表的最后一个节点
while current.next:
current = current.next
current.next = new_node
# 当头部节点不存在时
else:
self.head = new_node
# 判断链表是否为空
def is_empty(self):
return bool(self.head)
# 向链表任意位置插入元素
def insert(self, position, value):
self._length = None
new_node = Node(value)
# 判断插入位置是否在链表的索引范围内
if position < 0 or position > self.get_length():
raise IndexError('Out of linked list range.')
# 当插入位置为头节点时
if position == 0:
new_node.next, self.head = self.head, new_node
return
# 当插入位置不在头节点时,遍历链表找到插入位置,插入新节点
current = self.head
i = 0
while i < position:
pre, current = current, current.next
i += 1
pre.next, new_node.next = new_node, current
# 删除指定位置的节点
def remove(self, position):
self._length = None
# 判断删除位置是否在链表的索引范围内
if position < 0 or position > self.get_length() - 1:
raise IndexError('Position out of linked list range.')
i = 0
current = self.head
# 当删除位置为头节点时
if position == i:
self.head, current.next = self.head.next, None
return
# 当删除位置不是头节点时,遍历链表找到删除位置
i += 1
prev, current = current, current.next
while i != position:
prev, current = current, current.next
i += 1
prev.next, current.next = current.next, None
# 获取链表长度
def get_length(self):
if self._length is not None:
return self._length
current = self.head
length = 0
while current is not None:
length += 1
current = current.next
self._length = length
return length
# 遍历链表,并依次打印节点数据
def print_list(self):
print('linked_list:')
current = self.head
while current is not None:
print(current.data)
current = current.next
# 将链表反转
def reverse(self):
prev = None
current = self.head
while current is not None:
# next_node = current.next
# current.next = prev
# prev = current
# current = next_node
next_node, current.next = current.next, prev
prev, current = current, next_node
self.head = prev
# 将列表转换为链表
def init_list(self, data_list):
self.head = Node(data_list[0])
current = self.head
for item in data_list[1:]:
node = Node(item)
current.next, current = node, node
# 替换指定位置的节点
def replace(self, position, value):
# 判断替换位置是否在链表索引范围内
if position < 0 or position > self.get_length() - 1:
raise IndexError("Position out of linked-list range.")
_node = Node(value)
_current = self.head
i = 0
if position == 0:
_node.next, self.head = self.head.next, _node
_current.next = None
else:
i += 1
_prev, _current = _current, _current.next
while i != position:
i += 1
_prev, _current = _current, _current.next
_prev.next, _node.next = _node, _current.next
_current.next = None
# 返回给定值的第一个节点索引
def index(self, value):
_current = self.head
i = 0
while _current is not None:
if _current.data == value:
return i
i += 1
_current = _current.next
return -1
def __getitem__(self, position):
if position < 0 or position > self.get_length() - 1:
raise IndexError("Position out of linked-list range.")
_current = self.head
i = 0
while i != position:
_current = _current.next
i += 1
return _current.data
数据结构学习--单链表(python)的更多相关文章
- 数据结构学习--单循环链表(python)
概念 将单链表的终端节点的指针由原来的空指针改为指向头节点, 就是整个单链表形成一个环, 这种首尾相接的单链表称为单循环链表. 实现 class Node: """ 节点 ...
- Python数据结构之单链表
Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...
- javascript数据结构之单链表
下面是用javascript实现的单链表,但是在输出的时候insert方法中存在问题,chrome的console报错说不能读取空的属性,调试了很久都没有通过,先在这里存着,以后再来修改一下. //数 ...
- 数据结构之单链表的实现-java
一.单链表基本概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元(一般是非连续存储单元)存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素data + 指针next ...
- python 数据结构之单链表的实现
链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...
- 【数据结构】单链表介绍及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 ...
随机推荐
- easywechat微信开发SDK之小微商户进件(二)
正式开始进件之前需要准备几个东西 1.服务商商户号 2.API密钥 微信服务商后台中设置 3.APIv3密钥 微信服务商后台中设置 4.API证书路径 登录服务商后台下载 生成证书官方又文档的 很 ...
- Integer的比较==和String的比较==总结
一.序言 今天发现了一个很有趣的问题,在群里和朋友们讨论的也比较激烈,我现在给大家阐述一下问题. 二.发现问题 上代码... package com.hzwealth.test.question; p ...
- python初识-环境搭建,变量,条件,循环语句
1.python环境搭建: (1)安装Anaconda ,可选择非C盘安装: 注意:都勾选: (2)安装Pycharm 默认安装即可,安装过程同样都勾选: (3)破解Pycharm https://w ...
- 使用PaintCode便捷地实现动画效果
// // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import " ...
- 漫谈边缘计算(三):5G的好拍档
边缘计算的热度迅速攀升,还有一个不得不提的因素,就是5G的发展. [5G推动云计算从集中化向分布式演进] 在第一篇文章(<漫谈边缘计算(一):边缘计算是大势所趋>)中我提到,传统的云计算技 ...
- Python报错ERROR: Command errored out with exit status 1:
解决方法: 1.以管理员身份打开cmd 2.pip install robotframework-AutoItLibrary (本次安装时Python基于3.7.3,pip为最新版本) 3.安装成功
- Table表格滑过当前项[当前行][当前列]对应高亮
效果演示图: JS 代码如下: function TableHover($table){ $table.mouseenter(function(event) { va ...
- 将项目部署到github的方法
GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名GitHub. GitHub于2008年4月10日正式上线,除了Git代码仓库托管及基本的 We ...
- .Net Core使用分布式缓存Redis:数据结构
一.前言 本篇主要使用StackExchangeRedis在.Net Core中使用Redis,使用基础见:点击此处. 二.五种基础数据结构 1.字符串类型String 字符串类型是Redis中最基本 ...
- 笔记||Python3之列表与元组
列表List: 特性:①列表也是一种Squence类型 ②下标 ③能切片 ④可以存储任何类型的数据,每个元素是任意类型 ⑤内容可以改变:增删改查 1 -- 值 列表的元素值是可以改变的 a ...