# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Linked list

# ## Node

class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None

    def __str__(self):
        return "{} -> {}".format(self.data, self.next)


class LinkedList(object):
    def __init__(self):
        self.head = None
        self.tail = None
        self.counter = 0

    def __iter__(self):
        current = self.head
        while current:
            print(current.data)
            val = current.data
            current = current.next
            yield val

    def push_front(self, data):
        if data is None:
            return False
        node = Node(data)
        current = self.head
        if self.head:
            while current.next:
                current = current.next
            current.next = node
        else:
            self.head = node
            self.tail = node
        self.head = node
        self.counter += 1

    def push_back(self, data):
        node = Node(data)
        current = self.head
        if current is None:
            current.head = node
        else:
            while current.next is not None:
                current = current.next
            current.next = node


# ## Example

ll = LinkedList()
ll.push_front(10)
ll.push_front(2)
ll.push_front(3)
ll.push_front(4)
ll.push_back(50)
ll.push_front(100)

print("Count: \t", ll.counter)
print("Head: \t", ll.head.data)
print("Iter: \t", [i for i in ll])

StarcoderdataJupyterScriptsDedupFiltered