# 数据结构学习--双向链表(python)

### 实现

class Node:
def __init__(self, data):
self.data = data    # 数据域
self.next = None    # 指针域(直接后继)
self.prev = None    # 指针域(直接前驱)

"""
双向链表
"""

def __init__(self):

# 判断链表是否为空
def is_empty(self):

# 返回链表长度
@property
def size(self):
count = 0
while current is not None:
count += 1
current = current.next
return current

# 遍历链表
def travel(self):
while current is not None:
print(current.data)
current = current.next

# 在链表头部插入元素
new_node = Node(value)
if self.is_empty():
else:

# 在链表尾部插入元素
def append(self, value):
new_node = Node(value)
if self.is_empty():
else:
while _current.next is not None:
_current = _current.next
_current.next, new_node.prev = new_node, _current

# 查找元素是否存在
def search(self, value):
while _current is not None:
if _current.data == value:
return True
_current = _current.next
return False

# 在指定位置插入节点
def insert(self, position, value):
if position < 0 or position > self.size:
raise IndexError("Position out of range.")
if position == 0:
else:
_node = Node(value)
i = 0
while i != position:
i += 1
_current = _current.next
_prev = _current.prev
_prev.next, _node.prev = _node, _prev
_node.next, _current.prev = _current, _node

# 删除指定位置的节点
def remove(self, position):
if self.is_empty():
return None
if position < 0 or position > self.size - 1:
raise IndexError("Position out of range.")