数据结构——链表

一.简介  

  链表是一种物理存储上非连续,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性存储结构。由一系列节点组成的元素集合。每个节点包含两部分,
数据域item和指向下一个节点的指针next。通过节点之间的相互连接,最终串联成一个链表。
  链表中的每个节点包括两个部分:一个是存储数据元素的数据域;另一个是存储下一个节点的地址的指针域
  双向链表:双链表的每个节点有两个指针:一个指向后一个节点,另一个指向前一个节点。

二.Python实现

  ♦链表节点

class Node:
def __init__(self, item=None):
self.item = item
self.next = None

  ♦单向链表

class SingleLinkedList:
"""
单向链表
""" def __init__(self, heda=None):
self._head = heda
self.length = 0

  ♦添加节点

   头插法:从链表的头部(左端)插入

     

    def add_head(self, element):
"""
头插法:从头部插入
:param element: 需要添加进去的元素
:return:
"""
node = Node(element)
if self._head is None:
self._head = node
else:
node.next = self._head
self._head = node
self.length += 1

   尾插法:从链表的尾部(右端)插入

      

    def add_tail(self, element):
"""
尾插法:从尾部添加元素
:param element:需要添加进去的元素
:return:
"""
# 创建一个节点
node = Node(element)
if self._head is None:
self._head = node
else:
cur = self._head
# cur=node,node.next=None
while cur.next:
cur = cur.next
cur.next = node
return cur.next
self.length += 1

  ♦插入节点

       

    def insert(self, item: Node, index: int):
"""
往链表中指定位置插入值
:param item: 插入的节点
:param index: 插入的位置
:return:
"""
if index < 0 or index > self.length:
print('index out of range')
return
# 构建节点
if isinstance(item, Node):
node_insert = item
else:
node_insert = Node(item)
if index == 0:
node_insert.next = self._head
self._head = node_insert
else:
# 找到index的前一个节点
pre = self._head
for i in range(self.length):
if i == index - 1:
node_insert.next = pre.next
pre.next = node_insert
break
pre = pre.next
self.length += 1
return

  ♦删除节点

    

    def delete(self, index: int):
"""
删除指定位置的节点
:param index: 节点的位置
:return:
"""
# 判空
if self.is_empty():
print('empty chain')
return
if index < 0 or index > self.length:
print('index out of range')
return if index == 0:
self._head = self._head.next
else:
pre = self._head
for i in range(self.length):
if i == index - 1:
pre.next = pre.next.next
break
pre = pre.next
self.length -= 1
return

  ♦修改节点

    def update(self, item, index: int):
"""
修改节点item值
:param item: 修改之后的值
:param index:节点的位置
:return:
"""
# 判空
if self.is_empty():
print('empty chain')
return
if index < 0 or index >= self.length:
print('index out of range')
return node = self._head
for i in range(self.length):
if i == index:
node.item = item
return
node = node.next

   ♦获取节点

    def get_item(self, index: int):
"""
获取指定位置的节点item
:param index:指定位置
:return:item
"""
# 判空
if self.is_empty():
print('empty chain')
return
if index < 0 or index >= self.length:
print('index out of range')
return node = self._head
for i in range(self.length):
if i == index:
return node.item
node = node.next

  ♦遍历链表

    

    def traversal(self):
"""链表遍历"""
if self._head is None:
return
else:
cur = self._head
while cur:
print(cur.item)
cur = cur.next

  ♦反转链表

        

             

    def reverse(self):
"""
单向链表的反转:Input:1>2>3>4>5
Output:5>4>3>2>1
:return:
"""
if self._head is None or self.size() == 1:
return
else:
pre = None
cur = self._head
while cur is not None:
post = cur.next
cur.next = pre
pre = cur
cur = post
self._head = pre # 逆向后的头节点
self.traversal()

  ♦双向链表

class Node:
def __init__(self, item=None):
self.item = item
self.next = None
self.prior = None

  

  ♦双链表节点删除

      

  ♦双链表节点插入

      

 class Node:
def __init__(self, item=None):
self.item = item
self.next = None
self.prior = None class SingleLinkedList:
"""
单向链表
""" def __init__(self, heda=None):
self._head = heda
self.length = 0 def is_empty(self):
"""判断是否为空"""
return self.length == 0 def add_tail(self, element):
"""
尾插法:从尾部添加元素
:param element:需要添加进去的元素
:return:
"""
# 创建一个节点
node = Node(element)
if self._head is None:
self._head = node
else:
cur = self._head
# cur=node,node.next=None
while cur.next:
cur = cur.next
cur.next = node
return cur.next
self.length += 1 def add_head(self, element):
"""
头插法:从头部插入
:param element: 需要添加进去的元素
:return:
"""
node = Node(element)
if self._head is None:
self._head = node
else:
node.next = self._head
self._head = node
self.length += 1 def size(self): """
获取链表的大小
:return:int
"""
count = 0
if self._head is None:
return count else:
cur = self._head
while cur is not None:
count += 1
cur = cur.next
return count def insert(self, item: Node, index: int):
"""
往链表中指定位置插入值
:param item: 插入的节点
:param index: 插入的位置
:return:
"""
if index < 0 or index > self.length:
print('index out of range')
return
# 构建节点
if isinstance(item, Node):
node_insert = item
else:
node_insert = Node(item)
if index == 0:
node_insert.next = self._head
self._head = node_insert
else:
# 找到index的前一个节点
pre = self._head
for i in range(self.length):
if i == index - 1:
node_insert.next = pre.next
pre.next = node_insert
break
pre = pre.next
self.length += 1
return def delete(self, index: int):
"""
删除指定位置的节点
:param index: 节点的位置
:return:
"""
# 判空
if self.is_empty():
print('empty chain')
return
if index < 0 or index > self.length:
print('index out of range')
return if index == 0:
self._head = self._head.next
else:
pre = self._head
for i in range(self.length):
if i == index - 1:
pre.next = pre.next.next
break
pre = pre.next
self.length -= 1
return def update(self, item, index: int):
"""
修改节点item值
:param item: 修改之后的值
:param index:节点的位置
:return:
"""
# 判空
if self.is_empty():
print('empty chain')
return
if index < 0 or index >= self.length:
print('index out of range')
return node = self._head
for i in range(self.length):
if i == index:
node.item = item
return
node = node.next def get_item(self, index: int):
"""
获取指定位置的节点item
:param index:指定位置
:return:item
"""
# 判空
if self.is_empty():
print('empty chain')
return
if index < 0 or index >= self.length:
print('index out of range')
return node = self._head
for i in range(self.length):
if i == index:
return node.item
node = node.next def traversal(self):
"""链表遍历"""
if self._head is None:
return
else:
cur = self._head
while cur:
print(cur.item)
cur = cur.next def reverse(self):
"""
单向链表的反转:Input:1>2>3>4>5
Output:5>4>3>2>1
思路:先将head.next断开,即指向断开的元素pre(none);再将pre赋值head,将head赋值head.next;最后将head赋值pre
:return:
"""
if self._head is None or self.size() == 1:
return
else:
pre = None
cur = self._head
while cur is not None:
post = cur.next
cur.next = pre
pre = cur
cur = post
self._head = pre # 逆向后的头节点
self.traversal()

LinkedList

  

Python—数据结构——链表的更多相关文章

  1. Python 数据结构 链表

    什么是时间复杂度 时间频度:一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才知道.但是我们不可能也没有必要对每一个算法都进行上机测试,只需要知道那个算法花费的时间多,那个算法花费得 ...

  2. python数据结构链表之单向链表

    单向链表 单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域.这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值. 表元素域ele ...

  3. Python数据结构--链表

    class Node(): def __init__(self, dataval=None): self.dataval = dataval self.nextval = None class SLi ...

  4. Python数据结构——链表的实现

    链表由一系列不必在内存中相连的结构构成,这些对象按线性顺序排序.每个结构含有表元素和指向后继元素的指针.最后一个单元的指针指向NULL.为了方便链表的删除与插入操作,可以为链表添加一个表头. 删除操作 ...

  5. python数据结构与算法——链表

    具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...

  6. Python数据结构之单链表

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

  7. python数据结构之链表(一)

    数据结构是计算机科学必须掌握的一门学问,之前很多的教材都是用C语言实现链表,因为c有指针,可以很方便的控制内存,很方便就实现链表,其他的语言,则没那么方便,有很多都是用模拟链表,不过这次,我不是用模拟 ...

  8. python数据结构与算法

    最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...

  9. 算法之python创建链表实现cache

    算法之python创建链表实现cache 本节内容 问题由来 解决思路 实现代码 总结 1. 问题由来 问题起因于朋友的一次面试题,面试公司直接给出两道题,要求四十八小时之内做出来,语言不限,做出来之 ...

随机推荐

  1. LeetCode题解汇总(包括剑指Offer和程序员面试金典,持续更新)

    LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) LeetCode题解分类汇总(包括剑指Offer和程序员面试金典) 剑指Offer 序号 题目 难度 03 数组中重复的数字 简单 0 ...

  2. 中兴将用“加减乘除”建立理想 5G 网络

      6 月 28 日,MWC 2019 上海展期间,中兴通讯执行董事.总裁徐子阳发表演讲表示,面对 5G 建网大势,要看破大势,不破不立.为此中兴将用“加减乘除”建立理想 5G 网络. 何为“加减乘除 ...

  3. POJ 1330:Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20940   Accept ...

  4. 【SQL必知必会笔记(1)】数据库基础、SQL、MySQL8.0.16下数据库、表的创建及数据插入

    文章目录 1.数据库基础 1.1 数据库(database) 1.2 表(table) 1.3 列和数据类型 1.4 行 1.5 主键 2.什么是SQL 3.创建后续练习所需数据库.表(MySQL8. ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-qrcode

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. 创建一个简单的Spring应用

    环境已经安装完成,接下来创建一个简单的Spring应用. 创建Spring应用步骤: 创建一个maven项目 添加spring库依赖 创建Bean类 添加Bean的xml装配文件 创建主类 运行应用程 ...

  7. 黑马oracle_day01:02.oracle的基本操作

    01.oracle体系结构 02.oracle的基本操作 03.oracle的查询 04.oracle对象 05.oracle编程 02.oracle的基本操作 PLSQL中文乱码问题解决1.查看服务 ...

  8. 使用Linux系统,是一种什么体验?

    导读 同事,从事嵌入式软件开发多年,主要开发环境用的就是linux,最疯狂的一段时间直接把系统装成linux系统,然后在linux下面虚拟一个windows操作系统,主要有些事情必须在windows才 ...

  9. 从华硕裁员、分拆业务看传统PC企业转型到底有多难?

    近段时间,华硕的处境可谓"冰火两重天".一方面,华硕正式发布ROG游戏手机.这款手机以超强性能和华丽外观,让游戏玩家群体为之沸腾.即使最高售价高达12999元,还是有不少玩家趋之若 ...

  10. maven 依赖报错

    1 maven项目,在Intellij 右侧 Maven projects - Lifecycle - clean , validate, compile, ….,右击clean,选中Run ‘pro ...