双向链表(golang)
可以使用Python中的类来实现双向链表,具体实现方法可以参考以下代码:
```python
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
new_node.prev = current
def prepend(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
self.head.prev = new_node
new_node.next = self.head
self.head = new_node
def print_list(self):
current = self.head
while current:
print(current.data)
current = current.next
```
以上代码实现了双向链表的基本操作,包括添加节点、在头部添加节点和打印链表。