单向链表
class Node:
def __init__(self,val):
self.val = val
self.next = None
class Singlelinklist:
def __init__(self,node=None):
self.__head = node
# 判断链表是否为空
def is_empty(self):
return self.__head == None
# 获取链表长度
def length(self):
l = 0
cur = self.__head
while cur:
l += 1
cur = cur.next
return l
# 遍历链表
def travel(self):
cur = self.__head
while cur:
print(f'{cur.val}->',end='')
cur = cur.next
print('None')
# 头插法
def add(self,item):
node = Node(item)
node.next = self.__head
self.__head = node
# 尾插法
def append(self,item):
cur = self.__head
node = Node(item)
# 判断当前链表是否为空,为空就直接让head指向node节点
if self.is_empty():
self.__head = node
while cur.next != None:
cur = cur.next
cur.next = node
# 指定位置添加元素
def insert(self,index,item):
# 如果index>=链表长度,就使用尾插发
if index >= self.length():
self.append(item)
return
# index<=0就使用头插法
if index <= 0:
self.add(item)
return
i = 1
cur = self.__head
node = Node(item)
while i < index:
cur = cur.next
i += 1
# 跳出循环之后,cur指向的就是index位置的前一个位置
# 现将node的next指向cur的next
# 再将cur的next指向node
node.next = cur.next
cur.next = node
# 移除节点
def remove(self,item):
cur = self.__head
pre = None
while cur:
# 判断当前节点的val是否等于item
if cur.val == item:
# 判断当前节点是否是头结点
if cur == self.__head:
self.__head = cur.next
else:
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
# 查询元素
def search(self,item):
cur = self.__head
while cur:
if cur.val == item:
return True
cur = cur.next
return False
单向循环链表
class Node:
def __init__(self,val):
self.val = val
self.next = None
class Sincyclinklist:
def __init__(self):
self.__head = None
# 判断链表是否为空
def is_empty(self):
return self.__head == None
# 获取链表长度
def length(self):
cur = self.__head
if self.is_empty():
return 0
else:
l = 1
while cur.next != self.__head:
l += 1
cur = cur.next
return l
# 遍历链表
def travel(self):
cur = self.__head
if self.is_empty():
print('None')
else:
while cur.next != self.__head:
print(f'{cur.val}->', end='')
cur = cur.next
print(cur.val)
# 头插法
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
# 跳出循环,cur指向的就是最后一个节点
node.next = self.__head
self.__head = node
cur.next = node
# 尾插法
def append(self, item):
cur = self.__head
node = Node(item)
# 判断当前链表是否为空,为空就直接让head指向node节点
# 在让node.next指向自己或者self.__head都是可以的
if self.is_empty():
self.__head = node
node.next = node
else:
while cur.next != self.__head:
cur = cur.next
# 跳出循环,cur指向的就是最后一个节点
cur.next = node
node.next = self.__head
# 指定位置添加元素
def insert(self, index, item):
# 如果index>=链表长度,就使用尾插法
if index >= self.length():
self.append(item)
return
# index<=0就使用头插法
if index <= 0:
self.add(item)
return
i = 1
cur = self.__head
node = Node(item)
while i < index:
cur = cur.next
i += 1
# 跳出循环之后,cur指向的就是index位置的前一个位置
# 先将node.next指向cur.next
# 再将cur.next指向node
node.next = cur.next
cur.next = node
# 删除节点
def remove(self, item):
if self.is_empty():
return
cur = self.__head
pre = None
while cur.next != self.__head:
# 判断当前节点的val是否等于item
if cur.val == item:
# 判断当前节点是否是头结点
if cur == self.__head:
# 创建一个新的游标,让它指向最后一个节点
new_cur = self.__head
while new_cur.next != self.__head:
new_cur = new_cur.next
# 最后一个节点再指向cur.next
# 让self.__head等于cur.next,也就是指向cur.next
new_cur.next = cur.next
self.__head = cur.next
else:
pre.next = cur.next
return
# 如果当前节点不是要删除的元素,pre和cur就同时往后移动
else:
pre = cur
cur = cur.next
# 跳出循环之后,还有最后一个节点没有做判断
# 删除最后一个节点
if cur.val == item:
# 判断当前链表是否只有一个节点
# 只有一个节点的时候是进入不了上面的循环,所以说当前的pre还是为None,None是没有next这个属性的
# 所以下面的判断依据也可以改成if not pre:
if self.length() == 1:
self.__head == None
else:
pre.next = self.__head
# 查询元素
def serach(self, item):
# 判断是否为空
if self.is_empty():
return False
cur = self.__head
while cur.next != self.__head:
if cur.val == item:
return True
cur = cur.next
if cur.val == item:
return True
return False
双向链表
class Node:
def __init__(self,val):
self.val = val
self.next = None
self.pre = None
class Doublelinklist:
def __init__(self,node=None):
self.__head = node
# 判断链表是否为空
def is_empty(self):
return self.__head == None
# 获取链表长度
def length(self):
l = 0
cur = self.__head
while cur:
l += 1
cur = cur.next
return l
# 遍历链表
def travel(self):
cur = self.__head
while cur:
# 只是为了让打印结果看起来像一个链表,你怎么打印都行
print(f'{cur.val}<->', end='')
cur = cur.next
print('None')
# 头插法
def add(self,item):
node = Node(item)
if self.is_empty():
self.__head = node
else:
node.next = self.__head
# 把第一个节点的pre指向node
self.__head.pre = node
# 再将self.__head指向node
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
node.pre = cur
# 按位置插入
def insert(self,index,item):
# 如果index>=链表长度,就使用尾插发
if index >= self.length():
self.append(item)
return
# index<=0就使用头插法
if index <= 0:
self.add(item)
return
i = 0
cur = self.__head
node = Node(item)
while i < index:
cur = cur.next
i += 1
# 跳出循环之后,cur指向的就是index位置的节点
# 把新节点的pre指向cur.pre
node.pre = cur.pre
# 把新节点的next指向cur
node.next = cur
# 再将cur.pre.next指向node
cur.pre.next = node
# 最后将node.pre指向node
node.pre = node
def remove(self,item):
cur = self.__head
while cur:
# 判断当前节点的值是否等于item
if cur.val == item:
# 判断当前节点是否是第一个节点
if cur == self.__head:
self.__head = cur.next
cur.next.pre = None
else:
cur.pre.next = cur.next
# 加一个判断,是因为要删除的节点是最后一个时,就不用执行下面这一步了
# 也就是直接让prev.next指向cur.next,这时的cur.next就是None
if cur.next:
cur.next.pre = cur.pre
return
else:
cur = cur.next
def serach(self,item):
cur = self.__head
while cur:
if cur.val == item:
return True
cur = cur.next
return False