我不明白使用Python调试器时记录了什么。进入调试器后,我到了###Line where I get the output###
[代码中显示]之后的行。为什么我的输出不符合我的预期?我认为这可能是由于 repr 而链表是元素的容器。
我的输出
Value :2
Next: Value :3
Next: None
-----
-----
我预期的输出
Value :2
Next: 3
我的代码
element.py
class Element:
def __init__(self, value):
self.value = value
self.next = None
def __repr__(self):
string = "Value :" + str(self.value) + "\n" + "Next: " + str(self.next) + "\n" + "-----" + '\n'
return string
包含pdb
linked_list.py
from pdb import set_trace
from element import Element
class LinkedList:
def __init__(self, head=None):
self.head = head
def append(self, new_element):
current = self.head
if self.head:
while current.next:
current = current.next
current.next = new_element
else:
self.head = new_element
def get_position(self, position):
"""Get an element from a particular position.
Assume the first position is "1".
Return "None" if position is not in the list."""
# Handle bad input
if position <= 0 or type(position) is not int:
raise ValueError('Enter an integer that is greater than 0')
# Linked list is empty
if not self.head:
return None
counter = 0
current = self.head
while counter <= position and current:
counter += 1
if counter == position:
return current
current = current.next
return None
def insert(self, new_element, position):
"""Insert a new node at the given position.
Assume the first position is "1".
Inserting at position 3 means between
the 2nd and 3rd elements."""
set_trace()
if position == 1:
new_element.next = self.head.next
# print('pp', self.head.next.value)
###Line where I get the output###
self.head = new_element
return
element = self.get_position(position - 1)
new_element.next = element.next
element.next = new_element
def delete(self, value):
"""Delete the first node with a given value."""
# set_trace()
current = self.head
# first element has the value
if current.value == value:
self.head = current.next
return
prior = self.head
while current:
if current.value == value:
prior.next = current.next
prior = current
current = current.next
def __repr__(self):
# set_trace()
current = self.head
string = ""
counter = 1
while current:
string += str(counter) + ': ' + str(current.value) + '\n' + '-'* 10 + '\n'
current = current.next
counter += 1
return string
# def __repr__(self):
# pass
if __name__ == "__main__":
# Test cases
# Set up some Elements
e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)
# Start setting up a LinkedList
ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
print(ll)
# Test get_position
# Should print 3
print(ll.head.next.next.value)
# Should also print 3
print(ll.get_position(3).value)
# set_trace()
# Test insert
# set_trace()
ll.insert(e4,1)
print(ll)
# ll.insert(e4,3)
# # Should print 4 now
print(ll.get_position(3).value)
# # # Test delete
# ll.delete(1)
# # # Should print 2 now
# # # set_trace()
# print(ll.get_position(1).value)
# # Should print 4 now
# print(ll.get_position(2).value)
# # Should print 3 now
# print(ll.get_position(3).value)
# 3
# 3
# 4 # insert
# 2 # after delete
答案 0 :(得分:1)
3
是self.next.value
。你没有写str(self.next.value)
;你写了str(self.next)
。
self.next
是Element
,因此在Next:
之后,您会看到下一个元素的repr
。
# Part marked in parentheses is the repr of the next element.
Value :2
Next: (Value :3
Next: None)