Table of contents
One-way chain table example diagram
Second, the operation of one-way linked list
1. Determine whether the linked list is empty
2, the length of the linked list
3. Traverse the entire linked list
4. Add elements at the head of the linked list
5. Add elements at the end of the linked list
6. Insert an element at the specified position
7. Modify the element at the specified position
9. Find whether the element exists
related articles
Python one-way linked list operation
Operation of Python one-way circular linked list
Operation of Python doubly linked list
Operation of Python two-way circular linked list
1. One-way linked list
One-way chain table example diagram
There are two parts in the node node: elem (element) and next (pointer to the next node), the head pointer points to the head node
Second, the operation of one-way linked list
1. Determine whether the linked list is empty
def is_empty(self):
"""判断链表是否为空"""
if self.__head is None:
return True
else:
return False
2, the length of the linked list
As long as the current pointer does not point to None, add 1 for each traversal
def length(self):
"""链表长度"""
count = 0
cur = self.__head
while cur is not None:
count += 1
cur = cur.next
return count
3. Traverse the entire linked list
def travel(self):
"""遍历整个链表"""
cur = self.__head
while cur is not None:
print(cur.elem, end=' ')
cur = cur.next
print('')
4. Add elements at the head of the linked list
def add(self, item):
"""链表头部添加元素"""
node = Node(item)
if self.is_empty():
self.__head = node
else:
# 新来的节点的next指向第一个节点
node.next = self.__head
# 再改变第一个节点的指针指向新节点
self.__head = node
5. Add elements at the end of the linked list
def append(self, item):
"""链表尾部添加元素"""
# 创建新结点
node = Node(item)
# 是空链表就把头节点指向这个节点
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur.next is not None:
cur = cur.next
cur.next = node
6. Insert an element at the specified position
Traverse to find the previous node to be inserted
Modify the pointers of the previous node and the newly inserted node
Then modify the pointer of the newly inserted node and the pointer of the next node to point
def insert(self, pos, item):
"""在指定位置插入元素"""
# 位置pos在第一个元素之前,则在头部插入
if pos <= 0:
self.add(item)
# 位置pos大于总长度,则在尾部插入
elif pos > self.length():
self.append(item)
else:
# 指定位置添加元素
node = Node(item)
count = 0
pre = self.__head
# 循环定位指针位置
while count < (pos-1):
count += 1
pre = pre.next
node.next = pre.next
pre.next = node
7. Modify the element at the specified position
def modify(self, pos, item):
"""修改指定位置的元素"""
# 位置pos小于等于0时,则修改头部元素
if pos <= 0:
self.__head.elem = item
# 位置pos大于总长度,则修改尾部元素
elif pos >= self.length():
pre = self.__head
# 循环指针找到尾部元素
while pre is not None:
if pre.next is None: # pre.next为None说明已经找到尾部元素了
pre.elem = item
break
else:
pre = pre.next # 不是尾部元素就继续指针指向下一个
else:
count = 0
pre = self.__head
# 循环找出位置
while count < pos: # 1.当不满足count < pos条件时,说明指针已经指向了给定pos的位置
count += 1
pre = pre.next
pre.elem = item # 2.修改元素
8. Delete elements
def remove(self, item):
"""删除节点"""
cur = self.__head
pre = None
while cur is not None:
# 找到了要删除的元素
if cur.elem == item:
# 要删除的元素就是第一个元素,就把头指针指向当前的下一个节点
if not pre:
self.__head = cur.next
else:
pre.next = cur.next
break
# 未找到要删除的元素,指向向后走,继续遍历
else:
pre = cur
cur = cur.next
9. Find whether the element exists
def search(self, item):
"""查找节点是否存在"""
cur = self.__head
while cur is not None:
# 找到了返回True,未回到指向下一个继续遍历
if cur.elem == item:
return True
cur = cur.next
return False
3. Complete code
Details are in the notes
class Node():
def __init__(self, elem):
# 单链表结点
self.elem = elem
self.next = None
class SingleLinkList():
def __init__(self, node=None):
self.__head = node
def is_empty(self):
"""判断链表是否为空"""
if self.__head is None:
return True
else:
return False
def length(self):
"""链表长度"""
count = 0
cur = self.__head
while cur is not None:
count += 1
cur = cur.next
return count
def travel(self):
"""遍历整个链表"""
cur = self.__head
while cur is not None:
print(cur.elem, end=' ')
cur = cur.next
print('')
def add(self, item):
"""链表头部添加元素"""
node = Node(item)
if self.is_empty():
self.__head = node
else:
# 新来的节点的next指向第一个节点
node.next = self.__head
# 再改变第一个节点的指针指向新节点
self.__head = node
def append(self, item):
"""链表尾部添加元素"""
# 创建新结点
node = Node(item)
# 是空链表就把头节点指向这个节点
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur.next is not None:
cur = cur.next
cur.next = node
def insert(self, pos, item):
"""在指定位置插入元素"""
# 位置pos在第一个元素之前,则在头部插入
if pos <= 0:
self.add(item)
# 位置pos大于总长度,则在尾部插入
elif pos > self.length():
self.append(item)
else:
# 指定位置添加元素
node = Node(item)
count = 0
pre = self.__head
# 循环定位指针位置
while count < (pos-1):
count += 1
pre = pre.next
node.next = pre.next
pre.next = node
def modify(self, pos, item):
"""修改指定位置的元素"""
# 位置pos小于等于0时,则修改头部元素
if pos <= 0:
self.__head.elem = item
# 位置pos大于总长度,则修改尾部元素
elif pos >= self.length():
pre = self.__head
# 循环指针找到尾部元素
while pre is not None:
if pre.next is None: # pre.next为None说明已经找到尾部元素了
pre.elem = item
break
else:
pre = pre.next # 不是尾部元素就继续指针指向下一个
else:
count = 0
pre = self.__head
# 循环找出位置
while count < pos: # 1.当不满足count < pos条件时,说明指针已经指向了给定pos的位置
count += 1
pre = pre.next
pre.elem = item # 2.修改元素
def remove(self, item):
"""删除节点"""
cur = self.__head
pre = None
while cur is not None:
# 找到了要删除的元素
if cur.elem == item:
# 要删除的元素就是第一个元素,就把头指针指向当前的下一个节点
if not pre:
self.__head = cur.next
else:
pre.next = cur.next
break
# 未找到要删除的元素,指向向后走,继续遍历
else:
pre = cur
cur = cur.next
def search(self, item):
"""查找节点是否存在"""
cur = self.__head
while cur is not None:
# 找到了返回True,未回到指向下一个继续遍历
if cur.elem == item:
return True
cur = cur.next
return False
if __name__ == '__main__':
ll = SingleLinkList()
print(ll.is_empty())
print(ll.length())
ll.travel()
print('append')
ll.append(10)
ll.append(12)
ll.travel()
print('add')
ll.add(9)
ll.travel()
print('insert')
ll.insert(1, 33)
ll.travel()
print('remove')
ll.remove(9)
ll.travel()
print('modify')
ll.modify(-1, 13)
ll.travel()
ll.modify(10, 44)
ll.travel()
ll.modify(1, 55)
ll.travel()
print('search')
print(ll.search(12))