예제 #1
0
def test_to_list():
    cdll = CircularDoublyLinkedList()
    node_1 = Node(RandomString.make())
    cdll.insert_at_beg(node_1)
    node_2 = Node(RandomString.make())
    cdll.insert_after(node_1, node_2)
    node_3 = Node(RandomString.make())
    cdll.insert_after(node_2, node_3)
    assert cdll.to_list() == [node_1.data, node_2.data, node_3.data]
예제 #2
0
def test_remove_from_end():
    cdll = CircularDoublyLinkedList()
    node_1 = Node(RandomString.make())
    node_2 = Node(RandomString.make())
    node_3 = Node(RandomString.make())
    cdll.insert_at_beg(node_3)
    cdll.insert_at_beg(node_2)
    cdll.insert_at_beg(node_1)
    assert cdll.to_list() == [node_1.data, node_2.data, node_3.data]
    cdll.remove(node_3)
    assert cdll.to_list() == [node_1.data, node_2.data]
예제 #3
0
def test_all_inserts():
    cdll = CircularDoublyLinkedList()
    node_1 = Node(RandomString.make())
    node_2 = Node(RandomString.make())
    node_3 = Node(RandomString.make())
    node_4 = Node(RandomString.make())
    cdll.insert_at_beg(node_1)
    cdll.insert_after(node_1, node_2)
    cdll.insert_at_end(node_4)
    cdll.insert_before(node_4, node_3)
    assert cdll.to_list() == [
        node_1.data, node_2.data, node_3.data, node_4.data
    ]
예제 #4
0
def test_get_node():
    cdll = CircularDoublyLinkedList()
    node_1 = Node(RandomString.make())
    node_2 = Node(RandomString.make())
    node_3 = Node(RandomString.make())
    cdll.insert_at_beg(node_1)
    assert node_1 == cdll.get_node(0)
    assert cdll.get_node(1) is None
    cdll.insert_after(node_1, node_2)
    cdll.insert_after(node_2, node_3)
    assert node_2 == cdll.get_node(1)
    assert node_3 == cdll.get_node(2)
    assert cdll.get_node(3) is None
    assert cdll.get_node(999) is None
예제 #5
0
class Circle:
    def __init__(self):
        """
        First, the marble numbered 0 is placed in the circle... This marble is designated the current marble.
        """
        self._data = CircularDoublyLinkedList()
        node = Node(0)
        self._data.insert_at_beg(node)
        self.current = node

    def move(self, n):
        """
        Move n positions around the circle. n can be positive (clockwise) or negative (counter-clockwise).
        Return the node at the new position.
        """
        node = self.current
        while n:
            if n > 0:
                node = node.next
                n -= 1
            else:
                node = node.prev
                n += 1
        return node

    def place(self, val):
        node = Node(val)
        to = self.move(1)
        self._data.insert_after(to, node)
        self.current = node

    @property
    def current(self):
        return self._current

    @current.setter
    def current(self, value):
        self._current = value

    def remove(self, node):
        self._data.remove(node)
        # Make sure current stays valid.
        # Did we remove the only node, or the current node?
        if self._data.first == None:
            self.current = None
        elif self.current == node:
            self.current = node.next

    def __repr__(self):
        return f"Circle(data: {repr(self._data)}, current node: {self.current.data})"
예제 #6
0
def test_one_node():
    cdll = CircularDoublyLinkedList()
    node = Node(RandomString.make())
    cdll.insert_at_beg(node)
    assert cdll.to_list() == [node.data]