在python3环境下,写了一段关于单链表的操作程序,主要包括:判断链表是否为空、链表的长度、遍历链表元素、链表添加节点的三种方式:尾插法、头插法和任意位置插入法、最后做了两道关于链表的笔试题,分别是实现链表的逆序和从无序链表中去除重复项具体代码实现如下:
节点
class Node():
def __init__(self, x):
self.data = x
self.next = None
链表
class Link():
def __init__(self, node=None):
self.__head = node
def is_empty(self):
"""
判断一个链表是否为空
:return: True or False
"""
return self.__head is None
def link_len(self):
"""
检查链表的长度
:return: 链表的长度
"""
count = 0
cur = self.__head
while cur:
count += 1
cur = cur.next
return count
def append(self, elm):
"""
从尾部插入节点
:param elm: 节点的值
:return:
"""
node = Node(elm)
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur.next is not None:
cur = cur.next
cur.next = node
def add(self, elm):
"""
从头部插入节点
:param elm: 节点的数值
:return:
"""
node = Node(elm)
if self.is_empty():
self.__head = node
else:
node.next = self.__head
self.__head = node
def insert(self, elm, pos):
"""
从指定位置插入节点
:param elm: 节点的数值
:param pos: 指定位置
:return:
"""
node = Node(elm)
if self.is_empty():
self.__head = node
elif pos > (self.link_len() - 1):
self.append(elm)
elif pos <= 0:
self.add(elm)
else:
count = 0
cur = self.__head
while cur is not None:
if count == pos:
node.next = cur.next
cur.next = node
break
else:
count += 1
cur = cur.next
def travel(self):
"""
遍历一个链表
:return: 打印链表的值
"""
cur = self.__head
while cur:
print(cur.data, end=" ")
cur = cur.next
print("")
def reverse(self):
"""
实现链表的逆序2:给定一个带头节点的链表,请将其逆序。
即如果单链表原来是:head -> 1 -> 2 -> 3,
那么逆序后是:head -> 3 -> 2 -> 1
:return:
"""
if self.is_empty():
return
else:
cur = self.__head
pre = None
nex = cur.next
while cur.next is not None:
cur.next = pre
pre = cur
cur = nex
nex = cur.next
cur.next = pre
pre = cur
self.__head = pre
def reverse_2(self):
"""
链表的逆序2,插入法
假设原链表是:head -> 1 -> 2 -> 3,
在遍历到2时:head -> 2 -> 1 -> 3
...
:return:
"""
if self.is_empty():
return
else:
cur = self.__head
nex = cur.next
while cur.next is not None:
cur.next = nex.next
nex.next = self.__head
self.__head = nex
nex = cur.next
def remove_the_same(self):
"""
从无序链表中移除重复项:给定一个没有排序的链表,去掉重复项,并保留原顺序
例如:1 -> 3 -> 1 -> 5 -> 5 -> 7,去掉重复项后:1 -> 3 -> 5 -> 7
:return:
"""
if self.is_empty():
return
else:
cur = self.__head
while cur is not None:
pre = cur
nex = cur.next
while nex is not None:
if nex.data == cur.data:
pre.next = nex.next
nex = nex.next
else:
pre = nex
nex = nex.next
cur = cur.next
self.travel()
if __name__ == '__main__':
l = Link()
for i in range(5):
l.append(i)
print(l.is_empty()) # False
print(l.link_len()) # 5
l.travel() # 0 1 2 3 4
l.reverse()
l.travel() # 4 3 2 1 0
l.reverse_2()
l.travel() # 0 1 2 3 4
# l.append(1)
# l.append(3)
# l.append(1)
# l.append(5)
# l.append(5)
# l.append(7)
# l.travel() # 1 3 1 5 5 7
# l.remove_the_same() # 1 3 5 7
# l.add(1)
# l.travel() # 1
# l.add(2)
# l.add(3)
# l.append(4)
# l.travel() # 3 2 1 4
# l.insert(5, 10)
# l.travel() # 3 2 1 4 5
# l.insert(6, -1)
# l.travel() # 6 3 2 1 4 5
# l.insert(7, 2)
# l.travel() # 6 3 2 7 1 4 5
对实现链表的逆序(两种方式)和从无序链表中去除重复项做如下简单说明:
**移除无序链表中的重复项,**只是从最常规的思路进行求解的,当然这种方式并不是最好的方式,(当后面的节点中没有和当前的节点重复的节点的时候,程序还是会对其遍历,浪费了资源,大家可以对其纠正),这里画了一个简单的示意图,有助于大家对程序的理解:
至于上面其他的程序,这里就不过多的说明了,画个链表图,很容易的。