本文实例讲述了Python双链表原理与实现方法。分享给大家供大家参考,具体如下:
class Node(object):
def __init__(self, value=None, prev=None, next=None):
self.value = value# 节点数据域
self.prev = prev# 节点前驱指针
self.next = next# 节点后继指针
在双链表类的构造方法中定义头指针以及链表长度属性
class doubleLinked(object):
def __init__(self):
self.head = Node()# 头指针节点,用于确定链表第一个节点,不计入链表实际长度
self.length = 0
通过实例属性self.length是否为0判断链表是否为空
# 判断链表是否为空
def is_Empty(self):
return self.length == 0
根据传入的value值创建node节点对象
判断是否为空,若为空则插入节点的前驱节点为head头指针节点,head头指针指向node
如果链表非空:
# 尾部添加
def append(self, value):
node = Node(value)
if self.length == 0:
node.prev = self.head
self.head.next = node
else:
curnode = self.head.next
while curnode.next != None:
curnode = curnode.next
curnode.next = node
node.prev = curnode
self.length += 1
调用类方法is_Empty()判断是否为空,若为空则直接调用append()方法
当链表非空时
# 头部添加
def add(self, value):
if self.is_Empty():
self.append(value)
node = Node(value)
curnode = self.head.next
self.head.next = node
node.next = curnode
curnode.prev = node
self.length += 1
老样子,依旧要先判断原链表是否为空,为空的时候返回False
链表不为空时:
# 删除表头节点
def remove(self):
if self.is_Empty():
return False
curnode = self.head.next
self.head = self.head.next
self.head.next = curnode.next
self.length -= 1
接收两个位置参数,postion和value
创建待插入节点对象
变量curnode存放当前节点,变量i初始值为2(位置参数<2时,默认插入到第二个位置,>2时,通过while循环找到指定位置节点再进行插入)
找到指定位置后,待插入节点的后驱指针指向当前节点curnode的后继节点,待插入节点的前驱节点为当前节点。
当前节点的原后驱节点的前驱指针域指向待插入节点,当前节点curnode的后驱节点变更为插入节点
链表长度+1
# 插入到指定位置
def insert(self, postion, value):
node = Node(value)
curnode = self.head.next
i = 2
while i < postion:
i += 1
curnode = curnode.next
node.next = curnode.next
node.prev = curnode
curnode.next.prev = node
curnode.next = node
self.length += 1
依旧需要判断是否为空,为空时返回False
链表不为空时:
# 删除指定节点
def delete(self, value):
if self.is_Empty():
return False
curnode = self.head.next
while curnode.value != value:
curnode = curnode.next
curnode.prev.next = curnode.next
curnode.next.prev = curnode.prev
curnode.next = curnode
self.length -= 1
class Node(object):
def __init__(self, value=None, prev=None, next=None):
self.value = value
self.prev = prev
self.next = next
class doubleLinked(object):
def __init__(self):
self.head = Node()
self.length = 0
def __iter__(self):
for node in self.iter_node():
yield node.value
# 对链表进行可迭代操作
def iter_node(self):
curnode = self.head.next
while curnode.next != None:
yield curnode
curnode = curnode.next
if curnode.next == None:
yield curnode
# 判断链表是否为空
def is_Empty(self):
return self.length == 0
# 尾部添加
def append(self, value):
node = Node(value)
if self.length == 0:
node.prev = self.head
self.head.next = node
else:
curnode = self.head.next
while curnode.next != None:
curnode = curnode.next
curnode.next = node
node.prev = curnode
self.length += 1
# 头部添加
def add(self, value):
if self.is_Empty():
self.append(value)
node = Node(value)
curnode = self.head.next
self.head.next = node
node.next = curnode
curnode.prev = node
self.length += 1
# 插入到指定位置
def insert(self, postion, value):
node = Node(value)
curnode = self.head.next
i = 2
while i < postion:
i += 1
curnode = curnode.next
node.next = curnode.next
node.prev = curnode
curnode.next.prev = node
curnode.next = node
self.length += 1
# 删除表头节点
def remove(self):
if self.is_Empty():
return False
curnode = self.head.next
self.head = self.head.next
self.head.next = curnode.next
self.length -= 1
# 删除指定节点
def delete(self, value):
if self.is_Empty():
return False
curnode = self.head.next
while curnode.value != value:
curnode = curnode.next
curnode.prev.next = curnode.next
curnode.next.prev = curnode.prev
curnode.next = curnode
self.length -= 1
# 测试
linkedlist = doubleLinked()
print(linkedlist.is_Empty())
linkedlist.append(1)
linkedlist.append(3)
linkedlist.append(5)
linkedlist.add(4)
linkedlist.add(2)
linkedlist.insert(3,10)
linkedlist.remove()
linkedlist.delete(3)
# 遍历打印
i = 1
for node in linkedlist:
print("第%d个链表节点的值: %d"%(i, node))
i += 1
运行结果:
True
第1个链表节点的值: 4
第2个链表节点的值: 10
第3个链表节点的值: 1
第4个链表节点的值: 5
希望本文所述对大家Python程序设计有所帮助。
Powered By python教程网 鲁ICP备18013710号
python博客 - 小白学python最友好的网站!