在理解了算法之后,我在Python中为双链表编写了一个程序。但是只有功能的某些部分正在工作。这是我的代码
class Node(object):
def __init__(self,d):
self.next_node = None
self.prev_node = None
self.data = d
class DoublyLinkedList(object):
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def add(self,d):
new_node = Node(d)
if self.tail:
self.tail.next_node = new_node
new_node.next_node = None
new_node.prev_node = self.tail
else:
self.head = new_node
self.tail = new_node
new_node.prev_node = None
self.size+=1
def addBeg(self,d):
new_node = Node(d)
current_node = self.head
current_node.prev_node = new_node
new_node.prev_node = None
new_node.next_node = current_node
self.head = new_node
self.size+=1
def add_at(self,d,index):
new_node = Node(d)
previous_node = None
current_node = self.head
i = 0
while i<index and current_node:
previous_node = current_node
current_node = current_node.next_node
i+=1
#once it reaches the desired index
if i==index:
previous_node.next_node = new_node
new_node.prev_node = previous_node
new_node.next_node = current_node
current_node.prev_node = new_node
self.size+=1
return True
else:
return False #list ain't too long
def remove(self,d):
previous_node = None
current_node = self.head
while current_node:
if current_node.data == d:
if previous_node: #the node is somewhere in between
previous_node.next_node = current_node.next_node
current_node.next_node.prev_node = previous_node
else:
#it is the first node
self.head = current_node.next_node
current_node.next_node.prev_node = None
self.size -= 1
return True
previous_node = current_node
current_node = current_node.next_node
return False
def search(self,d):
current_node = self.head
while current_node:
if current_node.data == d:
return True
current_node = current_node.next_node
return False
#traverse through the linked list and store the elements in a list
def to_list(self):
lis = []
current_node = self.head
while current_node:
lis.append(current_node.data)
current_node = current_node.next_node
return lis
dll = DoublyLinkedList()
当我尝试使用add函数插入多个元素时,它表现得很奇怪。例如,如果我尝试添加三个元素,它只显示第一个和最后一个
ll.add(40)
ll.add(50)
ll.add(60)
print(ll.to_list())
我得到输出为[40,60]
第二个问题是如果我删除了其他内容而只保留一个元素
#removed all other elements
ll.add(70)
我得到'没有属性'错误。发生了什么事?
答案 0 :(得分:1)
添加功能时出现问题,忘记更新 self.tail 。
def add(self,d):
new_node = Node(d)
if self.tail:
self.tail.next_node = new_node
new_node.next_node = None
new_node.prev_node = self.tail
self.tail=new_node #change is here
else:
self.head = new_node
self.tail = new_node
new_node.prev_node = None
self.size+=1
删除最后一个节点的另一个问题。
def remove(self,d):
previous_node = None
current_node = self.head
while current_node:
if current_node.data == d:
if previous_node: #the node is somewhere in between
#print(d)
previous_node.next_node = current_node.next_node
if current_node.next_node is None: #deleting last node
current_node.prev_node=None
else:
current_node.next_node.prev_node = previous_node
else:
#it is the first node
self.head = current_node.next_node
current_node.next_node.prev_node = None
self.size -= 1
return True
previous_node = current_node
current_node = current_node.next_node
return False
Working Code.希望这会有所帮助。