##### 标题：Python - 高级链表

• 双链表包含称为第一个和最后一个的链接元素。
• 每个链接都有一个数据字段和两个称为next和prev的链接字段。
• 每个链接都使用其下一个链接与其下一个链接链接。
• 每个链接都使用其上一个链接与之前的链接链接。
• 最后一个链接将链接作为空来标记列表的结尾。

## 创建双向链表

```class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None

def __init__(self):

def push(self, NewVal):
NewNode = Node(NewVal)

# Print the Doubly Linked list
def listprint(self, node):
while (node is not None):
print(node.data),
last = node
node = node.next

dllist.push(12)
dllist.push(8)
dllist.push(62)
```

```62 8 12
```

## 插入双链表中

```# Create the Node class
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None

# Create the doubly linked list

def __init__(self):

# Define the push method to add elements
def push(self, NewVal):

NewNode = Node(NewVal)

# Define the insert method to insert the element
def insert(self, prev_node, NewVal):
if prev_node is None:
return
NewNode = Node(NewVal)
NewNode.next = prev_node.next
prev_node.next = NewNode
NewNode.prev = prev_node
if NewNode.next is not None:
NewNode.next.prev = NewNode

# Define the method to print the linked list
def listprint(self, node):
while (node is not None):
print(node.data),
last = node
node = node.next

dllist.push(12)
dllist.push(8)
dllist.push(62)
```

```62  8  13  12
```

## 附加到双向链表

```# Create the node class
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# Create the doubly linked list class

def __init__(self):

# Define the push method to add elements at the begining
def push(self, NewVal):
NewNode = Node(NewVal)

# Define the append method to add elements at the end
def append(self, NewVal):

NewNode = Node(NewVal)
NewNode.next = None
NewNode.prev = None
return
while (last.next is not None):
last = last.next
last.next = NewNode
NewNode.prev = last
return

# Define the method to print
def listprint(self, node):
while (node is not None):
print(node.data),
last = node
node = node.next

dllist.push(12)
dllist.append(9)
dllist.push(8)
dllist.push(62)
dllist.append(45)
```62 8 12 9 45