python数据结构链表之单向链表(实例讲解)
作者:jingxian 时间:2021-01-17 12:51:19
单向链表
单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域。这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值。
表元素域elem用来存放具体的数据。
链接域next用来存放下一个节点的位置(python中的标识)
变量p指向链表的头节点(首节点)的位置,从p出发能找到表中的任意节点。
节点实现
class Node(object):
"""单链表的结点"""
def __init__(self,item):
# item存放数据元素
self.item = item
# next是下一个节点的标识
self.next = None
单链表的操作
is_empty() 链表是否为空
length() 链表长度
travel() 遍历整个链表
add(item) 链表头部添加元素
append(item) 链表尾部添加元素
insert(pos, item) 指定位置添加元素
remove(item) 删除节点
search(item) 查找节点是否存在
单链表的实现
class Singlepnkpst(object):
"""单链表"""
def __init__(self):
self.__head = None
def is_empty(self):
"""判断链表是否为空"""
return self.__head == None
def length(self):
"""链表长度"""
# cur初始时指向头节点
cur = self.__head
count = 0
# 尾节点指向None,当未到达尾部时
while cur != None:
count += 1
# 将cur后移一个节点
cur = cur.next
return count
def travel(self):
"""遍历链表"""
cur = self.__head
while cur != None:
print(cur.item,end = ' ')
cur = cur.next
print("")
头部添加元素
def add(self, item):
"""头部添加元素"""
# 先创建一个保存item值的节点
node = Node(item)
# 将新节点的链接域next指向头节点,即_head指向的位置
node.next = self.__head
# 将链表的头_head指向新节点
self.__head = nod
尾部添加元素
def append(self, item):
"""尾部添加元素"""
node = Node(item)
# 先判断链表是否为空,若是空链表,则将_head指向新节点
if self.is_empty():
self.__head = node
# 若不为空,则找到尾部,将尾节点的next指向新节点
else:
cur = self.__head
while cur.next != None:
cur = cur.next
cur.next = node
指定位置添加元素
def insert(self, pos, item):
"""指定位置添加元素"""
# 若指定位置pos为第一个元素之前,则执行头部插入
if pos <= 0:
self.add(item)
# 若指定位置超过链表尾部,则执行尾部插入
epf pos > (self.length()-1):
self.append(item)
# 找到指定位置
else:
node = Node(item)
count = 0
# pre用来指向指定位置pos的前一个位置pos-1,初始从头节点开始移动到指定位置
pre = self.__head
while count < (pos-1):
count += 1
pre = pre.next
# 先将新节点node的next指向插入位置的节点
node.next = pre.next
# 将插入位置的前一个节点的next指向新节点
pre.next = node
删除节点
def remove(self,item):
"""删除节点"""
cur = self.__head
pre = None
while cur != None:
# 找到了指定元素
if cur.item == item:
# 如果第一个就是删除的节点
if not pre:
# 将头指针指向头节点的后一个节点
self.__head = cur.next
else:
# 将删除位置前一个节点的next指向删除位置的后一个节点
pre.next = cur.next
break
else:
# 继续按链表后移节点
pre = cur
cur = cur.next
查找节点是否存在
def search(self,item):
"""链表查找节点是否存在,并返回True或者False"""
cur = self.__head
while cur != None:
if cur.item == item:
return True
cur = cur.next
return False
标签:python,单向链表,数据结构,链表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Golang并发操作中常见的死锁情形
2023-07-03 13:17:43
javabean servlet jsp实现分页功能代码解析
2023-06-13 15:21:24
![](https://img.aspxhome.com/file/2023/2/63032_0s.png)
SQL Server用触发器强制执行业务规则
2009-01-20 16:05:00
Python的string模块中的Template类字符串模板用法
2023-02-02 10:53:05
css清除浮动的方法总结与选择
2008-06-06 12:58:00
Python中is和==的区别详解
2022-08-22 10:53:09
关于INDEX SERVER+ASP建立查询引擎的一点心得
2008-03-02 15:56:00
css被遮挡的绝对定位
2008-02-29 12:00:00
![](https://img.aspxhome.com/file/UploadPic/20082/29/2008229125054234s.gif)
从ThinkPHP3.2.3过渡到ThinkPHP5.0学习笔记图文详解
2023-09-04 10:21:28
![](https://img.aspxhome.com/file/2023/6/55586_0s.png)
python中的np.argmax() 返回最大值索引号
2022-08-07 14:22:57
![](https://img.aspxhome.com/file/2023/0/67860_0s.png)
JSON+JavaScript处理JSON的简单例子
2023-10-09 09:39:56
Python图像处理之直线和曲线的拟合与绘制【curve_fit()应用】
2021-01-28 10:30:49
![](https://img.aspxhome.com/file/2023/3/67353_0s.png)
python 爬取小说并下载的示例
2023-08-24 19:31:49
![](https://img.aspxhome.com/file/2023/2/61962_0s.gif)
css清除浮动“clear:both;”的应用
2008-03-17 12:54:00
什么是XML
2008-09-05 17:21:00
MySQL教程:Group By用法
2009-02-26 15:27:00
Python随机生成数模块random使用实例
2022-08-04 21:04:48
ASP.NET教程第一讲 ASP.NET简介
2007-08-07 11:50:00
asp中常用的字符串安全处理函数集合(过滤特殊字符等)
2011-02-20 10:40:00
window.top[_CACHE]实现多个jsp页面共享一个js对象
2023-07-18 09:25:14