标签:als info ima rap ges 链接 count func list
一种更复杂的链表是“双向链表”或“双面链表”。每个节点有两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值;而另一个指向下一个节点,当此节点为最后一个节点时,指向空值。
class Node(object):
"""双向链表节点"""
def __init__(self, item):
self.item = item
self.next = None
self.prev = None
class DLinkList(object):
"""双向链表"""
def __init__(self):
self._head = None
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.item,
cur = cur.next
print ""
def add(self, item):
"""头部插入元素"""
node = Node(item)
if self.is_empty():
# 如果是空链表,将_head指向node
self._head = node
else:
# 将node的next指向_head的头节点
node.next = self._head
# 将_head的头节点的prev指向node
self._head.prev = node
# 将_head 指向node
self._head = node
def append(self, item):
"""尾部插入元素"""
node = Node(item)
if self.is_empty():
# 如果是空链表,将_head指向node
self._head = node
else:
# 移动到链表尾部
cur = self._head
while cur.next != None:
cur = cur.next
# 将尾节点cur的next指向node
cur.next = node
# 将node的prev指向cur
node.prev = cur
def search(self, item):
"""查找元素是否存在"""
cur = self._head
while cur != None:
if cur.item == item:
return True
cur = cur.next
return False
指定位置插入节点
def insert(self, pos, item):
"""在指定位置添加节点"""
if pos <= 0:
self.add(item)
elif pos > (self.length()-1):
self.append(item)
else:
node = Node(item)
cur = self._head
count = 0
# 移动到指定位置的前一个位置
while count < (pos-1):
count += 1
cur = cur.next
# 将node的prev指向cur
node.prev = cur
# 将node的next指向cur的下一个节点
node.next = cur.next
# 将cur的下一个节点的prev指向node
cur.next.prev = node
# 将cur的next指向node
cur.next = node
删除元素
def remove(self, item):
"""删除元素"""
if self.is_empty():
return
else:
cur = self._head
if cur.item == item:
# 如果首节点的元素即是要删除的元素
if cur.next == None:
# 如果链表只有这一个节点
self._head = None
else:
# 将第二个节点的prev设置为None
cur.next.prev = None
# 将_head指向第二个节点
self._head = cur.next
return
while cur != None:
if cur.item == item:
# 将cur的前一个节点的next指向cur的后一个节点
cur.prev.next = cur.next
# 将cur的后一个节点的prev指向cur的前一个节点
cur.next.prev = cur.prev
break
cur = cur.next
测试
if __name__ == "__main__":
ll = DLinkList()
ll.add(1)
ll.add(2)
ll.append(3)
ll.insert(2, 4)
ll.insert(4, 5)
ll.insert(0, 6)
print "length:",ll.length()
ll.travel()
print ll.search(3)
print ll.search(4)
ll.remove(1)
print "length:",ll.length()
ll.travel()
class Node(object): #节点 def __init__(self,item): self.item = item self.prev = None self.next = None class DoubleLinkList(object): #双向链表 def __init__(self,node = None): self.__head = node #判空 def is_empty(self): return self.__head is None def length(self): #链表长度 #cur游标,用来移动遍历节点 #len记录数据 len = 0 cur = self.__head while cur is not None: len +=1 cur = cur.next return len def travel(self): #遍历 cur = self.__head while cur != None: print(cur.item,end =‘ ‘) cur = cur.next print(‘‘) def add(self,item): #在链表头部添加元素,头插法 node = Node(item) node.next = self.__head self.__head = node 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 is not 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: node = Node(item) cur = self.__head for i in range(pos-1): cur =cur.next node.prev = cur node.next = cur.next cur.next.prve = node cur.next = node def remove(self,item): #删除节点 node = Node(item) cur = self.__head while cur != None: if cur.item == item: if cur == self.__head: self.__head = cur.next if cur.next: #即cur.next is not None,即判断链表是否只有一个节点 cur.next.prev =None elif cur.next is None: cur.prev.next = None else: cur.prev.next = cur.next cur.next.prev = cur.prev break else: cur = cur.next def search(self,item): cur = self.__head while cur != None: if cur.item == item: return True else: cur = cur.next return False if __name__ == "__main__": ll = DoubleLinkList() 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) #8 1 2 3 4 5 6 ll.insert(-1,9) #9 8 1 2 3 4 5 6 ll.travel() ll.insert(3,100)#9 8 1 100 2 3 4 5 6 ll.travel() ll.insert(10,200) #9 8 1 100 2 3 4 5 6 200 ll.travel() ll.remove(8) ll.travel() ll.remove(200) ll.travel() ll.remove(9) ll.travel()
标签:als info ima rap ges 链接 count func list
原文地址:https://www.cnblogs.com/echo-kid-coding/p/11318132.html