【问题标题】:Can't display the elements of linked list properly after inserting the element to head将元素插入头部后无法正确显示链表的元素
【发布时间】:2019-06-03 17:41:13
【问题描述】:

我有一个链表,其中每个节点都保存着圆的数据(颜色、半径)。我有一个“形状”类,它有

(i) init :初始化 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 而不是更新的。

比如我要在头部位置插入的新节点是("Blue", 6),那么insert_circle(new_circle)就正确的插入头部位置。但是当我尝试打印 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()

【问题讨论】:

    标签: python linked-list


    【解决方案1】:

    这是因为您使用的属性名称以两个下划线开头,例如 __head__circle_list,而 Python 对此类名称有特殊规则。

    只使用一个下划线,或者不使用,应该没问题。

    【讨论】:

      【解决方案2】:

      python 解释器将以__(双下划线)开头的类成员的名称替换为_classname__membername,以确保该名称不会与另一个类中的相似名称重叠。

      在您的情况下,__head 属性被解释为_LinkedList__head

      当您调用insert_circle 方法时,self.__circle_list.__head = newNode 正在创建一个新的__head 属性,而不是重新分配_LinkedList__head

      您可以将set_head 方法添加到您的LinkedList

      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
      

      【讨论】:

        【解决方案3】:

        使用下面的代码在头部位置插入数据,

        def insert_circle(self,new_circle):
            self.__circle_list.insert(new_circle,None)
        

        显示列表:

        def get_circle_list(self):
            return self.__circle_list
        

        将上述函数添加到您的代码中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-25
          • 1970-01-01
          • 1970-01-01
          • 2021-09-05
          • 1970-01-01
          • 1970-01-01
          • 2018-08-02
          • 2013-02-17
          相关资源
          最近更新 更多