1.单链表
#定义单链表节点
class llistNode(object):
def __init__(self,elem):
self.elem = elem
self.next = None
#把LinkedListUnderflow定义成标准异常类ValueError的子类,在空表访问元素等场合抛出该异常
class LinkedListUnderflow(ValueError):
pass
#定义linklist类
class linklist(object):
def __init__(self):
self._head = None
#判断链表是否为空
def is_empty(self):
return self._head is None
def __len__(self):
"""
链表长度:
1.如果链表为空,直接返回0
2.如果链表不为空,依次遍历每个元素,计算长度
:return:
"""
if self.is_empty():
return 0
else:
cur = self._head
length = 0
while cur != None:
length += 1
cur = cur.next
return length
def printall(self):
"""遍历链表"""
p = self._head
while p is not None:
print(p.elem,end=' ')
p = p.next
print()
def prepend(self,elem):
"""在链表头插入数据"""
node = llistNode(elem)
node.next = self._head
self._head = node
def append(self,elem):
"""在链表尾部追加节点"""
if self._head is None:
self._head = llistNode(elem)
return
p = self._head
while p.next is not None:
p = p.next
p.next = llistNode(elem)
def insert(self,index,elem):
"""在指定位置插入元素"""
if index <= 0:
self.prepend(elem)
elif index >= len(self):
self.append(elem)
else:
node = llistNode(elem)
p = self._head
count = 0
while count <index-1:
count = count+1
p = p.next
node.next = p.next
p.next = node
def pop(self):
if self._head is None:
raise LinkedListUnderflow('in pop')
print('头结点的值:',self._head.elem)
print('头结点的next:',self._head.next)
e = self._head.elem
self._head = self._head.next
return e
"""根据item值删除元素"""
def remove(self,item):
pre = None
cur = self._head
if cur.elem == item:
self._head = self._head.next
else:
while cur:
if cur.elem != item:
pre = cur
cur = cur.next
else:
pre.next = cur.next
break
##测试代码
if __name__ == '__main__':
linklistp = linklist()
linklistp.prepend(1)
linklistp.prepend(2)
linklistp.prepend(3)
linklistp.prepend(4)
linklistp.insert(2,'python')
linklistp.printall()
print(len(linklistp))
linklistp.remove(3)
linklistp.printall()
print(len(linklistp))
leetcode中有个题目是:
删除链表的倒数第k个元素,思路是双指针实现。
from SingleLink import linklist
def solution(link,k):
if not link:
return
if k < 0 or k > len(link):
return
p = link._head
q = link._head
for i in range(k):
p = p.next
while p.next:
p = p.next
q = q.next
q.next = q.next.next
return link
#测试代码
if __name__ == '__main__':
linkp = linklist()
for i in range(8):
linkp.append(i)
linkp.printall()
print(len(linkp))
solution(linkp,7)
linkp.printall()
2.双链表
class Node(object):
def __init__(self,elem):
self.elem = elem
self.pre = None
self.next = None
def __str__(self):
return self.elem
class DuLinkList(object):
"""双向链表的封装"""
def __init__(self):
self._head = None
def is_empty(self):
return self._head == None
def __len__(self):
if self.is_empty():
return 0
cur = self._head
length = 0
while cur!=None:
length+=1
cur = cur.next
return length
def printall(self):
if not self.is_empty():
cur = self._head
while cur:
print(cur.elem,end=' ')
cur = cur.next
print()
else:
print('空链表')
def prepend(self,elem):
node = Node(elem)
if self.is_empty():
self._head = node
else:
node.next = self._head
self._head.pre = node
self._head = node
def append(self,elem):
node = Node(elem)
if self.is_empty():
self._head = node
else:
cur = self._head
while cur.next:
cur = cur.next
cur.next = node
node.pre = cur
def insert(self,index,elem):
"""在指定位置插入元素"""
if index <= 0:
self.prepend(elem)
elif index >= len(self):
self.append(elem)
else:
node = Node(elem)
p = self._head
count = 0
while count <index-1:
count = count+1
p = p.next
node.next = p.next
p.next = node
p.next.pre = node
node.pre = p
def remove(self, item):
if self.is_empty():
return
cur = self._head
length = 0
if cur.elem == item:
self._head = self._head.next
else:
while cur.elem != item:
cur = cur.next
length += 1
if length == len(self):
print('没有找到该元素')
else:
cur.pre.next = cur.next
cur.next.pre = cur.pre
#测试代码
if __name__ == '__main__':
dulinklistP = DuLinkList()
dulinklistP.prepend(3)
dulinklistP.prepend(4)
dulinklistP.prepend(2)
dulinklistP.append('linux')
dulinklistP.insert(2,'cpp')
dulinklistP.printall()
print(len(dulinklistP))
dulinklistP.remove(4)
dulinklistP.printall()
print(len(dulinklistP))