1、单链表的概念
在单链表中每个节点都是一个对象,包含数据项和下一个节点,对于单链表的操作主要是找到需要操作节点的前一个节点。单链表分有序和无序两种,无序直接添加到给定的位置,有序在插入时需要根据元素大小插入,也就是要找到它的位置。
2、无序链表python实现如下
class Node(object):
"""链表节点"""
def __init__(self, init_data):
self.data = init_data
self.next = None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self, new_data):
self.data = new_data
def set_next(self, new_next):
"""设置当前节点的下一个节点,非常有用"""
self.next = new_next
class UnorderedList(object):
"""无序链表"""
def __init__(self):
self.head = None
# 指向第一个节点的引用
def is_empty(self):
"""链表是否为空"""
return self.head is None
def add(self, item):
"""添加顺序与实际顺序相反,最后添加的在最前面"""
new_node = Node(item)
# 新节点
new_node.set_next(self.head)
# 新节点链接原始首节点(第一个节点),新节点在所有节点前面
self.head = new_node
# 头引用指向新的首节点
def size(self):
"""返回链表大小"""
count = 0
current = self.head
# 当前节点
while current is not None:
count += 1
current = current.get_next()
# 节点遍历
return count
def search(self, item):
"""查找某一项是否存在"""
current = self.head
# 从首节点开始遍历
found = False
# 是否已经找到
while (current is not None) and (not found):
if current.get_data() == item:
found = True
else:
current = current.get_next()
return found
def remove(self, item):
"""根据值删除项"""
current = self.head
# 从首节点开始遍历
previous = None
# 指向current的前一个节点
found = False
# 是否找到
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if found:
if previous is None:
self.head = current.get_next()
# 表示删除的是第一个节点
else:
previous.set_next(current.get_next())
return True
return False
def append(self, item):
"""添加到链表的最后"""
current = self.head
if current is None:
return False
while current.get_next() is not None:
current = current.get_next()
new_node = Node(item)
current.set_next(new_node)
def index(self, item):
"""返回指定项的索引"""
current = self.head
found = False
i = 0
while current is not None:
if current.get_data() == item:
found = True
break
else:
i += 1
current = current.get_next()
if found:
return i
else:
return -1
def pop(self):
"""删除最后一项并返回"""
if self.head is None:
return None
current = self.head
# 指向第一个节点的引用
length = self.size()
for i in range(1, length - 1):
current = current.get_next()
# 遍历到要删除节点的前一个
last = current.get_next()
if self.head.get_next() is None:
last_data = current.get_data()
self.head = None
# 只有一个节点特殊处理
else:
last_data = last.get_data()
current.set_next(last.get_next())
return last_data
def pop_pos(self, pos=1):
"""删除指定项并返回"""
if pos > self.size():
return None
current = self.head
previous = None
for i in range(1, pos):
previous = current
current = current.get_next()
data = current.get_data()
if previous is None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
return data
3、有序链表python实现如下
class OrderedList(object):
"""有序链表(默认升序)"""
def __init__(self):
self.head = None
# 指向第一个节点的引用
def is_empty(self):
"""链表是否为空"""
return self.head is None
def size(self):
"""返回链表大小"""
count = 0
current = self.head
# 当前节点
while current is not None:
count += 1
current = current.get_next()
# 节点遍历
return count
def remove(self, item):
"""根据值删除项"""
current = self.head
# 从首节点开始遍历
previous = None
# 指向current的前一个节点
found = False
# 是否找到
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if found:
if previous is None:
self.head = current.get_next()
# 表示删除的是第一个节点
else:
previous.set_next(current.get_next())
return True
return False
def search(self, item):
current = self.head
found = False
# 是否搜索到
stop = False
# 当在没有搜索到,但发现值大于搜索值,设为True
while (current is not None) and (not stop) and (not found):
if current.get_data() == item:
found = True
else:
if current.get_data() > item:
stop = True
else:
current = current.get_next()
return found
def add(self, item):
current = self.head
previous = None
stop = None
while (current is not None) and (not stop):
if current.get_data() > item:
stop = True
else:
previous = current
current = current.get_next()
new_node = Node(item)
if previous is None:
new_node.set_next(self.head)
self.head = new_node
else:
new_node.set_next(current)
previous.set_next(new_node)
def pop_pos(self, pos=1):
"""删除指定项并返回"""
if pos > self.size():
return None
current = self.head
previous = None
for i in range(1, pos):
previous = current
current = current.get_next()
data = current.get_data()
if previous is None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
return data
def pop(self):
"""删除最后一项并返回"""
if self.head is None:
return None
current = self.head
# 指向第一个节点的引用
length = self.size()
for i in range(1, length - 1):
current = current.get_next()
# 遍历到要删除节点的前一个
last = current.get_next()
if self.head.get_next() is None:
last_data = current.get_data()
self.head = None
# 只有一个节点特殊处理
else:
last_data = last.get_data()
current.set_next(last.get_next())
return last_data
def index(self, item):
"""返回指定项的索引"""
current = self.head
found = False
i = 0
while current is not None:
if current.get_data() == item:
found = True
break
else:
i += 1
current = current.get_next()
if found:
return i
else:
return -1