Может кто-нибудь объяснить, как self.nextNode.remove (data, self) вызывает метод remove ()? - PullRequest
0 голосов
/ 30 мая 2018
class node:
    def __init__(self,data):
        self.data=data
        self.nextNode=None
    def remove(self,data,previousNode):
        if self.data == data:
            previousNode.nextNode=self.nextNode
            del self.data
            del self.nextNode
        else:
            if self.nextNode is not None:
                self.nextNode.remove(data,self)

1 Ответ

0 голосов
/ 30 мая 2018

Я предполагаю, что это узел в связанном списке.

    def remove(self,data,previousNode):
        if self.data == data: # we hit the node we want to delete
            previousNode.nextNode=self.nextNode # we disconnect the current node from the link, connect the next node to its previous node
            del self.data # not exactly sure about these two lines
            del self.nextNode
        else: # we haven't hit the node we want to delete
            if self.nextNode is not None: # the list hasn't end, there is a "next"
                self.nextNode.remove(data, self)
                # this is the tricky part, 
                # you need to consider this in nextNode's perspective. 
                # You're calling nextNode's remove method, 
                # telling it the data you need to remove, and tell it 
                # that it's previousNode is your current node (self)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...