<commit_before><commit_msg>Linked list stack implementation + tests<commit_after>"""
    Minimal linked list implementation of a Stack
    ---------------------------------------------

    >>> s = Stack()
    >>> s.push(12)
    >>> s.push(16)
    >>> s.push(2)
    >>> s.pop()
    2
    >>> s.pop()
    16
    >>> s.pop()
    12
    >>> s = Stack([5, 6,7])
    >>> s.pop()
    7
    >>> s[1]
    5
    >>> s[0]
    6
    >>> list(s)
    [6, 5]
"""


class Node(object):
    __slots__ = ('data', 'nxt')
    def __init__(self, data, nxt=None):
        self.data = data
        self.nxt = nxt


class Stack(object):
    def __init__(self, lst=None):
        self._head = None
        self._length = 0
        self._index = None

        if lst:
            for d in lst:
                self.push(d)

    def push(self, data):
        node = Node(data, nxt=self._head)
        self._head = node
        self._length += 1
        return

    def pop(self):
        if self._length == 0:
            raise IndexError("pop from empty stack")
       
        if self._index == self._head:
            self._index = self._head.nxt

        data = self._head.data
        self._head = self._head.nxt
        self._length -= 1
        return data

    def __getitem__(self, key):
        if key >= self._length:
            raise IndexError("index out of range")

        current = self._head
        
        for _ in range(key):
            current = current.nxt

        return current.data

    def __iter__(self):
        self._index = self._head
        return self

    def __next__(self):
        if not self._index:
            raise StopIteration

        data = self._index.data
        self._index = self._index.nxt
        return data

    def __len__(self):
        return self._length



if __name__ == '__main__':
    import doctest
    doctest.testmod()
{'fraction_non_alphanumeric': 0.1445459430413756, 'fraction_numerical': 0.012896292315959162, 'mean_word_length': 1.4993288590604026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3569442', 'n_tokens_mistral': 597, 'n_tokens_neox': 568, 'n_words': 149}