Python数据结构与算法:第3-13课时:单向循环链表——查找、删除元素
查找元素:
实现代码;
def search(self, item):
"""查找节点是否存在"""
if self.is_empty():
return False
cur = self._head
while cur.next != self._head:
if cur.item == item:
return True
else:
cur = cur.next
return True
#这个if是用来判断刚刚好最后一个节点是要删除的对象的情况
if cur.item == item:
return True
return False
删除节点:
特殊情况,删除尾节点,需要重新建立循环连接!!!。
特殊情况2:若删除得是头节点,那么还要找到尾节点,把尾节点指向得地址修改。
实现代码;
def remove(self, item):
"""删除一个节点"""
# 若链表为空,则直接返回
if self.is_empty():
return
cur = self._head
pre = None
while cur.next != self._head:
if cur.item == item:
#首先判断目标节点是不是头节点
if cur == self._head:
#是头节点
#rear定位尾节点
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
#退出循环。cur指向尾节点
if cur.item == item:
if cur++self._head:
#链表只有一个节点
self._head = None
else:
pre.next = cur.next
测试;
class SingleNode(object):
"""单链表的结点"""
def __init__(self,item):
# _item存放数据元素
self.item = item
# _next是下一个节点的标识
self.next = None
class SinCycLinkedlist(object):
"""单向循环链表"""
def __init__(self,node = None):
self._head = None
if node:
node.next = node
def is_empty(self):
"""判断链表是否为空"""
return self._head == None
def length(self):
"""返回链表的长度"""
# 如果链表为空,返回长度0
if self.is_empty():
return 0
count = 1
cur = self._head
while cur.next != self._head:
count += 1
cur = cur.next
return count
def travel(self):
"""遍历链表"""
if self.is_empty():
return
#排除链表为空的特殊情况,防止陷入死循环
cur = self._head
while cur.next != self._head:
print(cur.item)
cur = cur.next
print(cur.item)
#尾节点还没有打印,必须补上一句。
def add(self, item):
"""头部添加节点"""
node = SingleNode(item)
if self.is_empty():
self._head = node
node.next = self._head
else:
# 添加的节点指向_head
node.next = self._head
# 移到链表尾部,将尾部节点的next指向node
cur = self._head
while cur.next != self._head:
cur = cur.next
cur.next = node
# _head指向添加node的
self._head = node
def append(self, item):
"""尾部添加节点"""
node = SingleNode(item)
if self.is_empty():
self._head = node
node.next = self._head
else:
# 移到链表尾部
cur = self._head
while cur.next != self._head:
cur = cur.next
# 将尾节点指向node
cur.next = node
# 将node指向头节点_head
node.next = self._head
def search(self, item):
"""查找节点是否存在"""
if self.is_empty():
return False
cur = self._head
while cur.next != self._head:
if cur.item == item:
return True
else:
cur = cur.next
return True
#这个if是用来判断刚刚好最后一个节点是要删除的对象的情况
if cur.item == item:
return True
return False
def remove(self, item):
"""删除一个节点"""
# 若链表为空,则直接返回
if self.is_empty():
return
cur = self._head
pre = None
while cur.next != self._head:
if cur.item == item:
#首先判断目标节点是不是头节点
if cur == self._head:
#是头节点
#rear定位尾节点
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
#退出循环。cur指向尾节点
if cur.item == item:
if cur == self._head:
#链表只有一个节点
self._head = None
else:
pre.next = cur.next
if __name__ == '__main__':
a = SinCycLinkedlist()
a.append(0)
a.append(1)
a.append(3)
a.append(9)
a.travel()
print('*******')
a.remove(3)
a.travel()
0
1
3
9
0
1
9