-2

Have tried to create a python Single Linked list , but i'm not able to create a iterator. Here is my code :

class LinkedList:
    def __init__(self):
        self._head=self
        self._tail=self
        self._size=0                    

    def __iter__(self):
        print 'Calling Iterator\n\n'
        _ListIterator(self._head)

class ListObj:
    def __init__(self,value):
        self._data=value
        self._pointingTo=None

class _ListIterator:
    def __init__(self,listHead):
        LIST=None
        self._curNode=listHead
        print dir(self._curNode)

    def __next__(self):
        if self._curNode._pointingTo is None:
            raise StopIteration
        else:
            item=self._curNode._data
            self._curNode=self._curNode._pointingTo
            return item

This iterator is failing by throwing an error as

TypeError: __iter__ returned non-iterator of type 'NoneType'
Dan Oberlam
  • 2,435
  • 9
  • 36
  • 54
SumitGupta
  • 23
  • 3
  • 1
    Do you really have to post the entire code? Please read http://www.sscce.org/ Also, please show your entire traceback. – thefourtheye May 12 '14 at 05:46
  • Ok , I got that i am passing just a single LinkedList object in the iterator , but how can i pass the complete list as a whole ! – SumitGupta May 12 '14 at 05:49
  • This is the entire traceback: Traceback (most recent call last): File "", line 1, in TypeError: __iter__ returned non-iterator of type 'NoneType' – SumitGupta May 12 '14 at 05:52
  • There is no reason to make the `ListIterator` object. Read the duplicate question and try that. –  May 12 '14 at 05:55
  • Yes , the linked helped,there is no point in making it a seperate object! Thanks Lego Stormtroopr – SumitGupta May 12 '14 at 06:11
  • FWIW: https://gist.github.com/therealprologic/a745f94c2ca2dd8bdba1 – James Mills May 12 '14 at 06:24

1 Answers1

1
_ListIterator(self._head)

You forgot to return this.

user2357112
  • 260,549
  • 28
  • 431
  • 505