请用Python语言帮我写一个双向链表
时间: 2023-02-21 21:47:22 浏览: 97
很高兴为您服务!这里是双向链表的Python代码:
# 定义一个节点
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# 定义双向链表
class DoublyLinkedList:
def __init__(self):
self.head = None
# 向链表中插入一个节点
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
if self.head is not None:
self.head.prev = new_node
self.head = new_node
# 在链表中删除一个节点
def deleteNode(self, key):
temp = self.head
if temp is not None:
if temp.data == key:
self.head = temp.next
temp = None
return
while temp is not None:
if temp.data == key:
break
prev = temp
temp = temp.next
if temp == None:
return
prev.next = temp.next
temp = None
阅读全文