实现常见的链表操作,插入头部,尾部,某数据之后或之前,修改,删除,排序,存储到文件,从文件构建链表,判空,是否存在,清空,排序,
时间: 2023-05-14 22:06:10 浏览: 83
c语言实现链表的基本操作,链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
以下是一个链表的基本操作实现,包括插入头部,尾部,某数据之后或之前,修改,删除,排序,存储到文件,从文件构建链表,判空,是否存在,清空,排序:
```python
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def insert_at_end(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def insert_after_node(self, prev_node, data):
if not prev_node:
print("Previous node is not in the list")
return
new_node = Node(data)
new_node.next = prev_node.next
prev_node.next = new_node
def insert_before_node(self, next_node, data):
if not next_node:
print("Next node is not in the list")
return
new_node = Node(data)
current_node = self.head
while current_node.next != next_node:
current_node = current_node.next
new_node.next = next_node
current_node.next = new_node
def modify_node(self, node, data):
if not node:
print("Node is not in the list")
return
node.data = data
def delete_node(self, key):
current_node = self.head
if current_node and current_node.data == key:
self.head = current_node.next
current_node = None
return
prev_node = None
while current_node and current_node.data != key:
prev_node = current_node
current_node = current_node.next
if current_node is None:
return
prev_node.next = current_node.next
current_node = None
def sort_list(self):
current_node = self.head
while current_node:
next_node = current_node.next
while next_node:
if current_node.data > next_node.data:
current_node.data, next_node.data = next_node.data, current_node.data
next_node = next_node.next
current_node = current_node.next
def save_to_file(self, filename):
with open(filename, 'w') as f:
current_node = self.head
while current_node:
f.write(str(current_node.data) + '\n')
current_node = current_node.next
def load_from_file(self, filename):
with open(filename, 'r') as f:
data = f.readline().strip()
while data:
self.insert_at_end(data)
data = f.readline().strip()
def is_empty(self):
return self.head is None
def is_node_in_list(self, node):
current_node = self.head
while current_node:
if current_node == node:
return True
current_node = current_node.next
return False
def clear(self):
self.head = None
def print_list(self):
current_node = self.head
while current_node:
print(current_node.data, end=' ')
current_node = current_node.next
print()
```
这个链表类可以用以下方式进行测试:
```python
linked_list = LinkedList()
linked_list.insert_at_beginning(5)
linked_list.insert_at_beginning(10)
linked_list.insert_at_end(15)
linked_list.insert_after_node(linked_list.head.next, 20)
linked_list.insert_before_node(linked_list.head.next.next, 25)
linked_list.print_list() # 10 5 25 20 15
linked_list.modify_node(linked_list.head.next, 30)
linked_list.print_list() # 10 30 25 20 15
linked_list.delete_node(25)
linked_list.print_list() # 10 30 20 15
linked_list.sort_list()
linked_list.print_list() # 10 15 20 30
linked_list.save_to_file('linked_list.txt')
linked_list.clear()
linked_list.load_from_file('linked_list.txt')
linked_list.print_list() # 10 15 20 30
```
阅读全文