我有一个链表,其中每个节点保存圆的数据(颜色、半径)。我有一个'Shape‘类,它有
(i) 初始化:初始化circle_list。
(ii) get_circle_list():返回circle_list
(iii) insert_circle(new_circle):在circle_list的头部位置插入新节点。
函数init insert_circle(new_circle)运行良好。但是,当我尝试使用get_circle_list()时,它返回旧的circle_list,而不是更新的use。
例如,我想在head位置插入的新节点是("Blue",6),然后insert_circle(new_circle)将它正确地插入到head位置。但是,当我尝试打印shape.get_circle_list().display()时,打印的是相同的旧circle_list。
PS:请不要将此代码与循环链表混淆,这是一个链表,它有代表圆的特征的节点,并且该列表的名称为circle_list。
class Node:
def __init__(self,data):
self.__data=data
self.__next=None
def get_data(self):
return self.__data
def set_data(self,data):
self.__data=data
def get_next(self):
return self.__next
def set_next(self,next_node):
self.__next=next_node
class LinkedList:
def __init__(self):
self.__head=None
self.__tail=None
def get_head(self):
return self.__head
def get_tail(self):
return self.__tail
def add(self,data):
new_node=Node(data)
if(self.__head is None):
self.__head=self.__tail=new_node
else:
self.__tail.set_next(new_node)
self.__tail=new_node
def insert(self,data,data_before):
new_node=Node(data)
if(data_before==None):
new_node.set_next(self.__head)
self.__head=new_node
if(new_node.get_next()==None):
self.__tail=new_node
else:
node_before=self.find_node(data_before)
if(node_before is not None):
new_node.set_next(node_before.get_next())
node_before.set_next(new_node)
if(new_node.get_next() is None):
self.__tail=new_node
else:
print(data_before,"is not present in the Linked list")
def display(self):
temp=self.__head
while(temp is not None):
print(temp.get_data())
temp=temp.get_next()
class Circle:
def __init__(self, color,radius):
self.__color=color
self.__radius=radius
def __str__(self):
return (self.__color+" "+str(self.__radius))
def get_color(self):
return self.__color
def get_radius(self):
return self.__radius
class Shape:
def __init__(self,circle_list):
self.__circle_list=circle_list
#Here is where the problem occurs
def get_circle_list(self):
return self.__circle_list
def insert_circle(self,new_circle):
newNode=Node(new_circle)
newNode.set_next(self.__circle_list.get_head())
self.__circle_list.__head=newNode
circle1=Circle("Red",4)
circle2=Circle("Green",5)
circle3=Circle("Purple",3.5)
new_circle=Circle("Blue",6)
circle_list=LinkedList()
circle_list.add(circle1)
circle_list.add(circle2)
circle_list.add(circle3)
shape=Shape(circle_list)
shape.insert_circle(new_circle)
#prints the same old circle_list here
shape.get_circle_list().display()
发布于 2019-06-04 02:28:34
这是因为您使用的是以两个下划线开头的属性名称,例如__head
和__circle_list
,而Python对这种名称有特殊的规则。
只需使用一个下划线,或者不使用下划线,就可以了。
发布于 2019-06-04 03:06:51
python解释器将以__
(双下划线)开头的类成员的名称替换为_classname__membername,以确保此名称不会与另一个类中的类似名称重叠。
在本例中,__head
属性被解释为_LinkedList__head
。
当您调用insert_circle
方法时,self.__circle_list.__head = newNode
正在创建一个新的__head
属性,而不是重新分配_LinkedList__head
。
您可以向LinkedList
添加一个set_head
方法
def set_head(self,new_head):
self.__head=new_head
然后在insert_circle
方法中调用它。
def insert_circle(self,new_circle):
newNode=Node(new_circle)
newNode.set_next(self.__circle_list.get_head())
self.__circle_list.set_head(newNode)
您还可以直接访问_LinkedList__head属性
def insert_circle(self,new_circle):
newNode=Node(new_circle)
newNode.set_next(self.__circle_list.get_head())
self.__circle_list._LinkedList__head=newNode
发布于 2021-02-13 18:32:15
使用下面的代码将数据插入头部位置。
def insert_circle(self,new_circle):
self.__circle_list.insert(new_circle,None)
要显示列表,请执行以下操作:
def get_circle_list(self):
return self.__circle_list
将上述函数添加到您的代码中。
https://stackoverflow.com/questions/56432150
复制相似问题