## 链表

### 单项链表

a, b = b, a的本质

python中变量是一块单独的空间, 其中保存的是所代表对象的地址

``````"""单项链表的实现"""
class Node(object):
def __init__(self, elem):
self.elem = elem
self.next = None
def __repr__(self):
return "<Node: {}>".format(self.elem)

def __init__(self, node=None):

node = Node(item)
else:

def append(self, item):
node = Node(item)
while cur.next is not None:
cur = cur.next
cur.next = node
else:

def travel(self):
while cur is not None:
print(cur.elem)
cur = cur.next

def remove(self, item):
while cur.next is not None:
if cur.next.elem == item:
cur.next = cur.next.next
break
cur = cur.next
else:
print("Not Exist")
else:
print("Not Exist")

def insert(self, pos, item):
i = 0
if pos == 0:
elif pos + 1 > self.length:
raise ValueError
else:
while cur.next is not None:
i += 1
if i == pos:
new_node = Node(item)
cur.next, new_node.next = new_node, cur.next
break
elif i > pos:
raise ValueError
cur = cur.next

def search(self, item):
while cur is not None:
if cur.elem == item:
print(cur.elem)
break
cur = cur.next

@property
def length(self):
count = 0
while cur is not None:
count += 1
cur = cur.next
return count

def is_empty(self):

### 循环链表

``````"""循环链表的实现"""
class Node(object):
def __init__(self, elem):
self.elem = elem
self.next = None
def __repr__(self):
return "<Node: {}>".format(self.elem)

def __init__(self, node=None):
if node is None:
else:

node = Node(item)
cur = cur.next
last = cur
else:

def append(self, item):
node = Node(item)
cur = cur.next
last = cur
else:

def travel(self):
print(cur.elem, end=" ")
cur = cur.next
print(cur.elem)

def remove(self, item):   # 单节点remove
cur = cur.next
last = cur

if cur.elem == item:
else:
if cur.next.elem == item:
cur.next = cur.next.next
break
cur = cur.next
else:
print("Not Exist")
else:
print("Not Exist")

def insert(self, pos, item):
length = self.length
if pos == 0:
elif pos == length:
self.append(item)
elif pos > length:
raise ValueError
else:
i = 0
i += 1
if i == pos:
node = Node(item)
cur.next, node.next = node, cur.next
break
cur = cur.next

def search(self, item):
if cur.elem == item:
print(cur.elem)
break
cur = cur.next
else:
if cur.elem == item:
print(cur.elem)

@property
def length(self):
count = 0
count += 1
cur = cur.next
count += 1
return count

def is_empty(self):

if __name__ == '__main__':
s.append(0)
s.append(1)
s.append(2)
s.append(3)
s.remove(0)
s.insert(3, 4)
s.search(1)
s.travel()``````

### 双向链表

``````"""双项链表的实现"""
class Node(object):
def __init__(self, elem):
self.pre = None
self.elem = elem
self.next = None
def __repr__(self):
return "<Node: {}>".format(self.elem)

def __init__(self, node=None):

node = Node(item)
else:

def append(self, item):
node = Node(item)
while cur.next is not None:
cur = cur.next
cur.next, node.pre = node, cur
else:

def travel(self):
while cur is not None:
print(cur.elem, end=" ")
cur = cur.next

def remove(self, item):
while cur.next is not None:
if cur.next.elem == item:
cur.next, cur.next.next.pre = cur.next.next, cur
break
cur = cur.next
else:
print("Not Exist")
else:
print("Not Exist")

def insert(self, pos, item):
i = 0
if pos == 0:
elif pos + 1 > self.length:
raise ValueError
else:
while cur.next is not None:
i += 1
if i == pos:
new_node = Node(item)
cur.next, new_node.next, new_node.pre = new_node, cur.next, cur
break
elif i > pos:
raise ValueError
cur = cur.next

def search(self, item):
while cur is not None:
if cur.elem == item:
print(cur.elem)
break
cur = cur.next

@property
def length(self):
count = 0
while cur is not None:
count += 1
cur = cur.next
return count

def is_empty(self):

### 跳表

#### 跳表的特点

• 跳表中的元素按照升序排列。
• 跳表中的每个节点包含多个指针，可以跨越多个节点进行快速查找。
• 跳表中的每个节点包含一个随机数，用于决定该节点的指针数量。

#### 跳表的优点：

• 跳表的实现简单，易于理解和实现。
• 跳表的插入、删除和查找操作的时间复杂度均为O(log n)，与平衡树类似。
• 跳表的空间复杂度较低，只需要额外存储每个节点的指针信息。

``````import random

class Node:
def __init__(self, key=None, value=None, level=0):
self.key = key
self.value = value
self.forward = [None] * (level + 1)

class SkipList:
def __init__(self):
self.level = 0

def search(self, key):
for i in range(self.level, -1, -1):
while node.forward[i] and node.forward[i].key < key:
node = node.forward[i]
node = node.forward[0]
if node and node.key == key:
return node.value
return None

def insert(self, key, value):
update = [None] * (self.level + 1)
for i in range(self.level, -1, -1):
while node.forward[i] and node.forward[i].key < key:
node = node.forward[i]
update[i] = node
node = node.forward[0]
if node and node.key == key:
node.value = value
else:
level = self._random_level()
if level > self.level:
for i in range(self.level + 1, level + 1):
self.level = level
node = Node(key, value, level)
for i in range(level + 1):
node.forward[i] = update[i].forward[i]
update[i].forward[i] = node

def delete(self, key):
update = [None] * (self.level + 1)
for i in range(self.level, -1, -1):
while node.forward[i] and node.forward[i].key < key:
node = node.forward[i]
update[i] = node
node = node.forward[0]
if node and node.key == key:
for i in range(self.level + 1):
if update[i].forward[i] != node:
break
update[i].forward[i] = node.forward[i]
while self.level > 0 and not self.head.forward[self.level]:
self.level -= 1

def _random_level(self):
level = 0
while random.random() < 0.5 and level < 16:
level += 1
return level``````