Sha256: 9a56439832ff7a3ea31966f83546f81fd043f660043fbd0af550ce193f157b81

Contents?: true

Size: 1.43 KB

Versions: 104

Compression:

Stored size: 1.43 KB

Contents

class Node(object):
    def __init__(self, value):
        self._value = value
        self._next = None

    def value(self):
        return self._value

    def next(self):
        return self._next


class LinkedIterator(object):
    def __init__(self, linked_list):
        self.current = linked_list._head

    def __iter__(self):
        return self

    def __next__(self):
        if self.current is None:
            raise StopIteration
        value = self.current.value()
        self.current = self.current.next()
        return value

    def next(self):
        return self.__next__()


class LinkedList(object):
    def __init__(self, values=[]):
        self._head = None
        self._len = 0
        [self.push(v) for v in values]

    def __iter__(self):
        return LinkedIterator(self)

    def __len__(self):
        return self._len

    def head(self):
        if self._head is None:
            raise EmptyListException("The list is empty")
        return self._head

    def push(self, value):
        newNode = Node(value)
        newNode._next = self._head
        self._head = newNode
        self._len += 1

    def pop(self):
        if self._head is None:
            raise EmptyListException("The list is empty")
        self._len -= 1
        ret = self._head.value()
        self._head = self._head.next()
        return ret

    def reversed(self):
        return LinkedList(self)


class EmptyListException(Exception):
    pass

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.179 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.178 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.177 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.176 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.175 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.174 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.173 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.172 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.171 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.170 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.169 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.167 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.166 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.165 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.164 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.163 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.162 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.161 tracks/python/exercises/simple-linked-list/example.py
trackler-2.2.1.160 tracks/python/exercises/simple-linked-list/example.py