算法基础_Third_Chapter
链表
1、单向链表
1.1、单向链表的操作
链表节点:
1.2、单链表定义和方法实现
class Node():
def __init__(self
,elem
):
self
.elem
= elem
self
.next = None
class SingleLinkList():
def __init__(self
,node
=None):
self
.__head
= node
def is_empty(self
):
return self
.__head
== None
def length(self
):
cur
= self
.__head
count
= 0
while cur
!= None:
count
+= 1
cur
= cur
.next
return count
def travel(self
):
cur
= self
.__head
while cur
!= None:
print(cur
.elem
,end
=" ")
cur
= cur
.next
def add(self
,item
):
node
= Node
(item
)
node
.next = self
.__head
self
.__head
= node
def append(self
,item
):
node
= Node
(item
)
if self
.is_empty
():
self
.__head
=node
else:
cur
= self
.__head
while cur
.next !=None:
cur
= cur
.next
cur
.next = node
def insert(self
,pos
,item
):
if pos
< 0:
self
.add
(item
)
elif pos
>(self
.length
()-1):
self
.append
(item
)
else:
pre
= self
.__head
count
= 0
while count
< (pos
-1):
count
+= 1
pre
= pre
.next
node
= Node
(item
)
node
.next = pre
.next
pre
.next = node
def remove(self
, item
):
node
= Node
(item
)
cur
= self
.__head
pre
= None
while cur
!=None:
if cur
.elem
== item
:
if pre
== None:
self
.__head
=cur
.next
else:
pre
.next = cur
.next
break
else:
pre
= cur
cur
= cur
.next
def search(self
,item
):
node
= Node
(item
)
cur
= self
.__head
while cur
!=None:
if cur
.elem
== item
:
return True
else:
cur
= cur
.next
return False
if __name__
== "__main__":
ll
= SingleLinkList
()
print(ll
.is_empty
())
print(ll
.length
())
ll
.append
(1)
print(ll
.is_empty
())
print(ll
.length
())
ll
.append
(2)
ll
.add
(8)
ll
.append
(3)
ll
.append
(4)
ll
.append
(5)
ll
.append
(6)
ll
.insert
(3,6)
ll
.insert
(100,23)
ll
.travel
()
ll
.remove
(6)
ll
.travel
()
1.3 链表与顺序表的对比
顺序表存取元素可通过访问地址一次定位(内存连续),发生动态改变,存储区需要改变,当存储量庞大无足够内存空间则无法解决。 链表的内存空间分散,利用空间增大,但是因为存储数值域和指针域,内存消耗增加,存取元素需用指针从前向后遍历。 时间复杂度:链表用于遍历,顺序表用于数据搬迁。
2、双向链表
2.1 双向链表的结构
2.2 双向链表的操作
class Node():
def __init__(self
,item
):
self
.elem
= item
self
.next = None
self
.prev
= None
class DoubleLinklist():
def __init__(self
, node
=None):
self
.__head
= node
def is_empty(self
):
return self
.__head
== None
def length(self
):
cur
= self
.__head
count
= 0
while cur
!= None:
count
+= 1
cur
= cur
.next
return count
def travel(self
):
cur
= self
.__head
while cur
!= None:
print(cur
.elem
, end
=" ")
cur
= cur
.next
def add(self
, item
):
node
= Node
(item
)
node
.next = self
.__head
self
.__head
= node
if node
.next:
node
.next.prev
= node
def append(self
, item
):
node
= Node
(item
)
if self
.is_empty
():
self
.__head
= node
else:
cur
= self
.__head
while cur
.next != None:
cur
= cur
.next
cur
.next = node
node
.prev
= cur
def insert(self
, pos
, item
):
if pos
< 0:
self
.add
(item
)
elif pos
> (self
.length
() - 1):
self
.append
(item
)
else:
cur
= self
.__head
count
= 0
while count
< pos
:
count
+= 1
cur
= cur
.next
node
= Node
(item
)
node
.next = cur
node
.prev
= cur
.prev
cur
.prev
.next = node
cur
.prev
= node
def remove(self
, item
):
node
= Node
(item
)
cur
= self
.__head
while cur
!= None:
if cur
.elem
== item
:
if cur
== self
.__head
:
self
.__head
= cur
.next
if cur
.next != None:
cur
.next.prev
= None
else:
cur
.prev
.next = cur
.next
if cur
.next:
cur
.next.prev
= cur
.prev
break
else:
cur
= cur
.next
def search(self
, item
):
node
= Node
(item
)
cur
= self
.__head
while cur
!= None:
if cur
.elem
== item
:
return True
else:
cur
= cur
.next
return False
if __name__
== "__main__":
dll
= DoubleLinklist
()
dll
.append
(1)
dll
.append
(2)
dll
.append
(5)
dll
.append
(6)
dll
.insert
(3,3)
dll
.travel
()
print()
dll
.remove
(3)
dll
.travel
()
2.3 单向循环列表
class Node():
def __init__(self
,elem
):
self
.elem
= elem
self
.next = None
class SingleLinkList():
def __init__(self
,node
=None):
self
.__head
= node
if node
:
node
.next = node
def is_empty(self
):
return self
.__head
== None
def length(self
):
cur
= self
.__head
if self
.is_empty
():
return 0
else:
count
= 1
while cur
.next != self
.__head
:
count
+= 1
cur
= cur
.next
return count
def travel(self
):
cur
= self
.__head
if self
.is_empty
():
return
else:
while cur
.next != self
.__head
:
print(cur
.elem
,end
=" ")
cur
= cur
.next
print(cur
.elem
)
def add(self
,item
):
node
= Node
(item
)
if self
.is_empty
():
self
.__head
= node
node
.next = node
else:
cur
= self
.__head
while cur
.next != self
.__head
:
cur
= cur
.next
node
.next = self
.__head
self
.head
= node
cur
.next = node
def append(self
,item
):
node
= Node
(item
)
if self
.is_empty
():
self
.__head
=node
node
.next = node
else:
cur
= self
.__head
while cur
.next != self
.__head
:
cur
= cur
.next
cur
.next = node
node
.next = self
.__head
def insert(self
,pos
,item
):
if pos
< 0:
self
.add
(item
)
elif pos
>(self
.length
()-1):
self
.append
(item
)
else:
pre
= self
.__head
count
= 0
while count
< (pos
-1):
count
+= 1
pre
= pre
.next
node
= Node
(item
)
node
.next = pre
.next
pre
.next = node
def remove(self
, item
):
cur
= self
.__head
pre
= None
if self
.is_empty
():
return
else:
while cur
.next!=self
.__head
:
if cur
.elem
== item
:
if cur
== self
.__head
:
rear
= self
.__head
while rear
.next != self
.__head
:
rear
= rear
.next
self
.__head
= cur
.next
rear
.next = self
.__head
else:
pre
.next = cur
.next
return
else:
pre
= cur
cur
= cur
.next
if cur
.elem
== item
:
if cur
== self
.__head
:
self
.__head
= None
else:
pre
.next = cur
.next
def search(self
,item
):
if self
.is_empty
():
return False
else:
cur
= self
.__head
while cur
.next != self
.__head
:
if cur
.elem
== item
:
return True
else:
cur
= cur
.next
if cur
.elem
== item
:
return True
return False
if __name__
== "__main__":
ll
= SingleLinkList
()
ll
.append
(1)
ll
.append
(2)
ll
.add
(1)
ll
.append
(3)
ll
.append
(4)
ll
.insert
(3,6)
ll
.insert
(100,23)
ll
.remove
(6)
print()
ll
.travel
()
ll
.remove
(23)
ll
.travel
()
ll
.remove
(1)
ll
.travel
()
转载请注明原文地址:https://ipadbbs.8miu.com/read-2534.html