一、单向链表概念
单向链表的链接方向是单向的,由结点构成,head指针指向第一个成为head结点,而终止于最后一个指向None的指针,对链表的访问要通过顺序读取从头部开始。
二、建立节点对象
1
2
3
4
|
class Node: def __init__( self ,data): self .data = data #节点的值域 self . next = None #连接下一个节点,暂时指向空 |
三、链表对象的初始定义
1
2
3
|
class linkList: def __init__( self ): self .head = None #首先建立链表头,暂时指向空 |
四、判断链表是否为空
1
2
3
4
5
6
|
#判断链表是否为空 def isEmpty( self ): if self .head: return False else : return True |
五、获取链表长度
1
2
3
4
5
6
7
8
9
10
|
def length( self ): if self .isEmpty(): return 0 else : t = self .head n = 1 while t. next : t = t. next n = n + 1 return n |
六、向头部添加节点
1
2
3
4
|
def addhead( self ,data): node = Node(data) #新建一个节点 node. next = self .head #新建的节点接上原来的链表 self .head = node #重置链表的头 |
七、向尾部添加节点
1
2
3
4
5
6
7
8
9
10
|
def addtail( self ,data): node = Node(data) #新建一个节点 #先判断链表是否为空 if self .isEmpty(): self .addhead(data) else : t = self .head while t. next : #通过循环找到尾部 t = t. next t. next = node #尾部接上 |
八、指定位置插入节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
def insert( self ,data,index): if index = = 0 or self .isEmpty(): self .addhead(data) elif index > = self .length(): self .addtail(data) else : node = Node(data) t = self .head n = 1 while n < index - 1 : t = t. next n = n + 1 a = t. next . next t. next = node node. next = a |
九、删除指定位置的节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
def delete( self ,index): if self .isEmpty(): print ( "The linked list is empty" ) else : t = self .head if index = = 0 : self .head = t. next elif index = = self .length() - 1 : n = 1 while n < self .length() - 1 : t = t. next n = n + 1 t. next = None elif index > self .length() - 1 : print ( "Out of range" ) elif index < 0 : print ( "Wrong operation" ) else : n = 1 while n < index - 1 : t = t. next n = n + 1 a = t. next . next t. next = a |
十、查找是否有该数据的节点
1
2
3
4
5
6
7
8
9
10
|
def search( self ,data): t = self .head n = 1 while t. next : if t.data = = data: print ( str (n) + " " ) t = t. next n = n + 1 if (t.data = = data): print ( str (n) + " " ) |
十一、遍历输出整个链表
1
2
3
4
5
6
7
8
9
|
def form( self ,datalist): self .addhead(datalist[ 0 ]) for i in range ( 1 , len (datalist)): self .addtail(datalist[i]) t = self .head while t. next : print (t.data) t = t. next print (t.data) |
十二、输入数据创建链表
1
2
3
4
5
6
7
8
9
|
def form( self ,datalist): self .addhead(datalist[ 0 ]) for i in range ( 1 , len (datalist)): self .addtail(datalist[i]) t = self .head while t. next : print (t.data) t = t. next print (t.data) |
十三、具体实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
data = input ( "input(以空格为界):" ) data = data.split( " " ) linkList = linkList() linkList.form(data) #创建链表 addlist = linkList.addhead( 5 ) #在头节点加入 linkList.ergodic() #遍历输出 addlist = linkList.addtail( 5 ) #在尾节点加入 linkList.ergodic() #遍历输出 linkList.search( 5 ) #查找是否有"5"的节点 linkList.delete( 4 ) #删除第4个数据 linkList.ergodic() #遍历输出 print (linkList.length()) #输出链表长度 linkList.insert( 89 , 2 ) #指定位置插入数据 linkList.ergodic() #遍历输出 |
到此这篇关于python中的单向链表实现的文章就介绍到这了,更多相关python单向链表内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_55073640/article/details/122736503