I wrote a class of simply linked list:

class ListNode: def __init__(self, v, nxt): self._value = v self._next = nxt def __repr__(self): return 'ListNode: Value: {0}, Next: {1}'.format(self._value, self._next) def __str__(self): return '({0}, {1})'.format(self._value, self._next) def getValue(self): return self._value def setValue(self, v): self._value = v def getNext(self): return self._next def setNext(self, v): self._next = v value = property(getValue, setValue) next = property(getNext, setNext) class ListNode2(ListNode): def __init__(self, v, nxt, prev): ListNode.__init__(self, v, nxt) self._previous = prev def __repr__(self): return 'ListNode: Value: {0}, Next: {1}, Previous: {2}'.format(self._value, self._next, self._previous) def __str__(self): return '({0}, {1}, {2})'.format(self._value, self._next, self._previous) def getPrevious(self): return self._previous def setPrevious(self, v): self._previous = v previous = property(getPrevious, setPrevious) class LinkedList: def __init__(self): self._head = None def getHead(self): return self._head head = property(getHead) def Add(self, node): node.next = self._head self._head = node def Remove(self): if self._head != None: self._head = self._head.next def __iter__(self): return self def next(self): current = self._head while current != None: yield current.Value current = current.next l = LinkedList() for i in range(10): l.Add(ListNode(i, None)) for data in l: # Надо, сделать так, чтобы так можно было перебирать весь список. print(data) 
  • The question is what? - Sergey Gornostaev
  • How to make the ability to iterate through the list of LinkedList class? - Vladislav Moldovan

1 answer 1

You, judging by the non-compliance with PEP8 and unnecessary gestures, came to Python from Java? First of all, you do not need a mediator in the form of a class LinkedList . Nodes themselves are able to answer for their condition and submit a linked list. As for the iteration itself, there are two ways - with an explicit iterator and with a generator.

First option:

 class LinkedListIterator: def __init__(self, item): self._item = item __iter__ = lambda self: self def __next__(self): if self._item is None: raise StopIteration item, self._item = self._item, self._item.next return item class Node: def __init__(self, value, next=None): self.value = value self.next = next def __str__(self): return str(self.value) def __add__(self, value): self.next = Node(value) return self.next def __iter__(self): return LinkedListIterator(self) 

The second option:

 class Node: def __init__(self, value, next=None): self.value = value self.next = next def __str__(self): return str(self.value) def __add__(self, value): self.next = Node(value) return self.next def __iter__(self): item = self while item is not None: yield item item = item.next 

And an example of use:

 head = Node(1) tail = head + 2 tail += 3 tail += 4 tail += 5 for i in head: print(i) 
  • From C # switched ... - Vladislav Moldovan
  • I almost guessed :) - Sergey Gornostaev
  • one
    @VladislavMoldovan easy head, head.next = head.next, None - Sergey Gornostaev
  • one
    @VladislavMoldovan Surplus abstractions are bad, lack is no better. Replacing LinkedList with Node is the second time. Throwing it away for the sake of "python way" you will not win anything, just add yourself a smut - vitidev
  • 2
    add to the iterator: __iter__ = lambda self: self . Aside: in pure Python, the use of explicit singly-linked lists is most likely an error (less readable, less effective code). - jfs