概念

链表(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)的更多相关文章

  1. 数据结构学习--单循环链表(python)

    概念 将单链表的终端节点的指针由原来的空指针改为指向头节点, 就是整个单链表形成一个环, 这种首尾相接的单链表称为单循环链表. 实现 class Node: """ 节点 ...

  2. Python数据结构之单链表

    Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...

  3. javascript数据结构之单链表

    下面是用javascript实现的单链表,但是在输出的时候insert方法中存在问题,chrome的console报错说不能读取空的属性,调试了很久都没有通过,先在这里存着,以后再来修改一下. //数 ...

  4. 数据结构之单链表的实现-java

    一.单链表基本概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元(一般是非连续存储单元)存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素data + 指针next ...

  5. python 数据结构之单链表的实现

    链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...

  6. 【数据结构】单链表介绍及leetcode206题反转单链表python实现

    题目传送门:https://leetcode-cn.com/problems/reverse-linked-list/ 文章目录 单链表介绍 链表 概念 种类 优缺点 单链表(slist) leetc ...

  7. 数据结构:单链表结构字符串(python版)添加了三个新功能

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  8. 数据结构:单链表结构字符串(python版)改进

    此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...

  9. 数据结构:单链表结构字符串(python版)

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

随机推荐

  1. 【Python3爬虫】网络小说更好看?十四万条书籍信息告诉你

    一.前言简述 因为最近微信读书出了网页版,加上自己也在闲暇的时候看了两本书,不禁好奇什么样的书更受欢迎,哪位作者又更受读者喜欢呢?话不多说,爬一下就能有个了解了. 二.页面分析 首先打开微信读书:ht ...

  2. FaceBook的秘钥散列获取

    随笔记录 先下载OpenSSL工具 在C盘创建一个openssl,将下好的OpenSSL工具解压到这里 将你的 .keystore文件复制到JAVA JDK 文件夹的bin目录里面(C:\Progra ...

  3. CCNA 之 七 路由协议 三 OSPF

    OSPF协议 OSPF开放式最短路径优先 全称:Open Shortest Path First 是目前使用最为广泛的路由协议,主要因为OSPF是开放式协议,和IGRP.EIGRP思科的私有协议不同. ...

  4. 浅谈ZooKeeper基本原理与源码分析

    最近一直有小伙伴私信我,问一些关于Zookeeper的知识,下边关于的Zookeeper的知识整理了一下,一起学习一下. 看完本文对于Zookeeper想深入全面了解的读者朋友们,小编这里整理了一份更 ...

  5. java数据类型(大小等),变量定义,各进制书写方法

    1. java中字符占两个字节,因为char类型占两个字节(16位),而C,C++中占1字节(8位). 2. 变量定义 第一步:声明(Declaration) 第二步:赋值(Assignment) 这 ...

  6. ThinkPHP5——Session和Cookie

    Session 首先要引入use think\Session:下面使用静态方法调用Session //赋值 Session::set('name','s1'); //赋值think作用域,set(‘名 ...

  7. JsonSchmea用法

    JsonSchmea用法 简介 JSON Schema是基于JSON格式,用于定义JSON数据结构以及校验JSON数据内容. JSON Schema官网地址:http://json-schema.or ...

  8. 揭秘 iOS App Extension 开发 —— Today 篇

    转自:http://www.cocoachina.com/ios/20160619/16760.html 本文授权转载,作者:Cyandev(简书) 从 iOS 8 开始,苹果引入了全新的 App E ...

  9. MVC效验器

    步骤一:导入依赖 <!--数据效验--> <dependency> <groupId>org.hibernate</groupId> <artif ...

  10. ansible批量管理常见的配置方法

    第7章 ansible的管理 7.1 ansible概念的介绍 ansible-playbook –syntax            检查语法 ansible-playbook -C         ...