Ejemplo n.º 1
0
def test_search_fail():
    linked_list = LinkedList()
    abc = a, b, c = list('abc')
    for i in abc:
        linked_list.insert(i)
    with pytest.raises(FileNotFoundError):
        linked_list.search('d')
Ejemplo n.º 2
0
def test_iterate():
    linked_list = LinkedList()
    for i in 'abc':
        linked_list.insert(i)
    j = -1
    for i in linked_list:
        j += 1
        assert i == 'abc'[::-1][j]
Ejemplo n.º 3
0
def test_previous():
    linked_list = LinkedList()
    abc = a, b, c = list('abc')
    for i in abc:
        linked_list.insert(i)
    assert linked_list.previous(a).item is b
    assert linked_list.previous(b).item is c
    assert linked_list.previous(c).item is None
Ejemplo n.º 4
0
def test_search():
    linked_list = LinkedList()
    abc = a, b, c = list('abc')
    for i in abc:
        linked_list.insert(i)
    assert linked_list.search(a).item is a
    assert linked_list.search(b).item is b
    assert linked_list.search(c).item is c
Ejemplo n.º 5
0
class AdjacencyList:
    def __init__(self, head: Vertex):
        self.head = head
        self.edgenodes = LinkedList()
        self.degree = 0

    def connect(self, edge: Edge):
        self.edgenodes.insert(Edgenode(head=self.head, edge=edge))
        self.degree += 1
Ejemplo n.º 6
0
def test_insert():
    linked_list = LinkedList()
    a, b = list('ab')
    assert linked_list.head.item is None
    assert linked_list.tail.item is None
    linked_list.insert(a)
    assert linked_list.head.item is a
    assert linked_list.head.next.item is None
    assert linked_list.tail.item is a
    linked_list.insert(b)
    assert linked_list.head.item is b
    assert linked_list.head.next.item is a
    assert linked_list.tail.item is a
Ejemplo n.º 7
0
def test_delete():
    linked_list = LinkedList()
    abc = a, b, c = list('abc')
    for i in abc:
        linked_list.insert(i)
    linked_list.delete(b)
    assert linked_list.head.item is c
    assert linked_list.head.next.item is a
    assert linked_list.tail.item is a
    linked_list.delete(a)
    assert linked_list.head.item is c
    assert linked_list.head.next.item is None
    assert linked_list.tail.item is c
    linked_list.delete(c)
    assert linked_list.head.item is None
    assert linked_list.tail.item is None
        print 'Wrong parameter provided, you asked to find 0 element'
        return
    first_node = alist.head
    last_node = alist.head
    # build a window spaced N
    for i in range(1, n):
        if last_node.next is None:
            print 'There are only {} in the list'.format(i)
            print alist
            return
        else:
            last_node = last_node.next
    # Move both pointers until we get to the end of the list
    while last_node.next is not None:
        last_node = last_node.next
        first_node = first_node.next
    # print N values found
    print 'Last {} elements in the list: '.format(n),
    for i in range(1, n + 1):
        print first_node.value,
        first_node = first_node.next


# Build a list with 10 elements for testing
ll = LinkedList()
for i in range(1, 21):
    ll.insert(random.randint(1, 100))
print 'Given list:',
print ll
find_last_N(ll, 1)
Ejemplo n.º 9
0
def test_delete_fail():
    linked_list = LinkedList()
    for i in 'abc':
        linked_list.insert(i)
    with pytest.raises(FileNotFoundError):
        linked_list.delete('d')