一.数组
1.python实现数组的增删查改
class Array:
def __init__(self, data):
self.__data = data
def find(self, index):
"""
index of data
:param index:
:return:
"""
if index > len(self.__data) or index < 0:
return False
else:
return self.__data[index]
def delete(self, index):
"""
delete function
:param index:
:return:
"""
if index > len(self.__data) or index < 0:
return False
else:
self.__data.pop(index)
return True
def insert(self, index, value):
"""
insert function
:param index:
:param value:
:return:
"""
if index > len(self.__data) or index < 0:
return False
else:
self.__data.insert(index, value)
return True
def insertToTail(self, value):
"""
insert data ,at tail
:param value:
:return:
"""
self.__data.append(value)
def printAll(self):
print(self.__data)
if __name__ == '__main__':
data=[1,2,3,4]
arr = Array(data)
print('=======origin data============')
arr.printAll()
print('==========find data======================')
print(arr.find(1))
print("==========insert data ============")
arr.insert(2,10)
arr.printAll()
print('========delete data==================')
arr.delete(3)
arr.printAll()
2.合并两个有序数组
https://leetcode-cn.com/problems/merge-sorted-array/submissions/
class Solution:
def merge(self, nums1, m, nums2, n):
while nums1[-1]==0:
if len(nums1)==m:
break
nums1.pop()
if nums1==[]:
break
nums1.extend(nums2)
nums1.sort()
return nums1
if __name__ == '__main__':
sol=Solution()
nums1=[1,2,3,0,0,0]
m=3
nums2=[2,5,6]
n=3
res=sol.merge(nums1,m,nums2,n)
print(res)
二.链表
链表分为 4 种情况:单链表,单循环链表,双链表,双循环链表.
其中:data存储当前节点数值;
next存储下一个节点的位置.
from typing import Optional
class Node:
def __init__(self, data, next_node=None):
self.data = data
self._next = next_node
class SinglyLinkedList:
def __init__(self):
self._head = None
def find_by_value(self, value):
p = self._head
while p and p.data != value:
p = p._next
return p
def find_by_index(self, index):
p = self._head
position = 0
while p and position != index:
p = p._next
position += 1
return p
def insert_value_to_head(self, value):
new_node = Node(value)
self.insert_node_to_head(new_node)
def insert_node_to_head(self, new_node):
if new_node:
new_node._next = self._head
self._head = new_node
def insert_value_after(self, node, value):
new_node = Node(value)
self.insert_node_after(node, new_node)
def insert_node_after(self, node, new_node):
if not node or not new_node:
return
new_node._next = node._next
node._next = new_node
def insert_value_before(self, node, value):
new_node = Node(value)
self.insert_node_before(node, new_node)
def insert_node_before(self, node, new_node):
if not self._head or not node or not new_node:
return
if self._head == node:
self.insert_node_to_head(new_node)
return
current = self._head
while current._next and current._next != node:
current = current._next
if not current._next: # node is not even in the list
return
new_node._next = node
current._next = new_node
def delete_by_node(self, node):
if not self._head or not node:
return
if node._next:
node.data = node._next.data
node._next = node._next._next
return
# node is the last one or not in the list
current = self._head
while current and current._next != node:
current = current._next
if not current: # node not in the list
return
current._next = node._next
def delete_by_value(self, value):
if not self._head or not value:
return
fake_head = Node(value + 1)
fake_head._next = self._head
prev, current = fake_head, self._head
while current:
if current.data != value:
prev._next = current
prev = prev._next
current = current._next
if prev._next:
prev._next = None
self._head = fake_head._next # in case head.data == value
def __repr__(self):
nums = []
current = self._head
while current:
nums.append(current.data)
current = current._next
return "->".join(str(num) for num in nums)
# 重写__iter__方法,方便for关键字调用打印值
def __iter__(self):
node = self._head
while node:
yield node.data
node = node._next
def print_all(self):
current = self._head
if current:
print(f"{current.data}", end="")
current = current._next
while current:
print(f"->{current.data}", end="")
current = current._next
print("\n", flush=True)
if __name__ == "__main__":
l = SinglyLinkedList()
for i in range(15):
l.insert_value_to_head(i)
print('==========initialization====================')
print('l:',l)
node9 = l.find_by_value(9)
print('============insert=========================')
l.insert_value_before(node9, 20)
print('l:', l)
l.insert_value_before(node9, 16)
l.insert_value_before(node9, 16)
print('============delete============================')
l.delete_by_value(16)
print('l:', l)
print('============find=============================')
node11 = l.find_by_index(3)
l.delete_by_node(node11)
l.delete_by_node(l._head)
l.delete_by_value(13)
print(l)
# for value in l:
# print(value)