示例#1
0
class Queue():
    """implementing queue functionality from LL"""
    def __init__(self, data):
        """init"""
        self.linked_list = LL(LLN(data))

    def __str__(self):
        """tostring method"""
        return str(self.linked_list).replace('Linked List', 'Queue')

    def enqueue(self, new_value):
        """enqueuing stuff onto list (onto top of LL))"""
        self.linked_list.insert(new_value)

    def dequeue(self):
        """takes the last thing off of the list"""
        curr = self.linked_list.head
        prev = None
        while curr.get_next():
            prev = curr
            curr = curr.get_next()

        if prev:
            prev.update_next(None)
            return curr
        else:
            raise ValueError("Cant remove last element of queue")
示例#2
0
def test_has_loop_circular_valid(one_four_ll):
    """
    test has loop with valid case of true circular
    """
    ll = LL([1, 2, 3, 4, 5, 6])
    ll.head._next._next = ll.head
    assert has_loop(ll) == True
示例#3
0
def test_check_valid_iterable():
    """
    check if iterable valid
    """
    with pytest.raises(TypeError) as err:
        LL(2)

    assert str(err.value) == 'Invalid iterable'
示例#4
0
class Stack():
    """implementing stack functionality as a linked list"""
    def __init__(self, data):
        """initialization"""
        self.linked_list = LL(LLN(data))

    def __str__(self):
        """tostring method"""
        return str(self.linked_list).replace('Linked List', 'Stack')

    def peek(self):
        """get top of stack"""
        return self.linked_list.head

    def pop(self):
        """popping off the last value (first value in LL)"""
        node = self.linked_list.head
        self.linked_list.head = self.linked_list.head.get_next()
        return node

    def push(self, new_value):
        """pushing stuff onto list (onto top of LL))"""
        self.linked_list.insert(new_value)
示例#5
0
 def __init__(self, data):
     """initialization"""
     self.linked_list = LL(LLN(data))
def predefined_ll():
    return LL([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
示例#7
0
class TestLinkedList(unittest.TestCase):
    def setUp(self):
        self.list = LL()

    def tearDown(self):
        self.list = None

    def test_insert(self):
        self.list.insert("David")

        self.assertTrue(self.list.head.get_data() == "David")
        self.assertTrue(self.list.head.get_next() is None)

    def test_insert_two(self):
        self.list.insert("David")
        self.list.insert("Thomas")

        self.assertTrue(self.list.head.get_data() == "Thomas")

        head_next = self.list.head.get_next()
        self.assertTrue(head_next.get_data() == "David")

    def test_nextNode(self):
        self.list.insert("Jacob")
        self.list.insert("Pallymay")
        self.list.insert("Rasmus")

        self.assertTrue(self.list.head.get_data() == "Rasmus")

        head_next = self.list.head.get_next()
        self.assertTrue(head_next.get_data() == "Pallymay")

        last = head_next.get_next()
        self.assertTrue(last.get_data() == "Jacob")

    def test_positive_search(self):
        self.list.insert("Jacob")
        self.list.insert("Pallymay")
        self.list.insert("Rasmus")

        found = self.list.search("Jacob")
        self.assertTrue(found.get_data() == "Jacob")

        found = self.list.search("Pallymay")
        self.assertTrue(found.get_data() == "Pallymay")

        found = self.list.search("Jacob")
        self.assertTrue(found.get_data() == "Jacob")

    def test_searchNone(self):
        self.list.insert("Jacob")
        self.list.insert("Pallymay")

        # make sure reg search works
        found = self.list.search("Jacob")

        self.assertTrue(found.get_data() == "Jacob")

        with self.assertRaises(ValueError):
            self.list.search("Vincent")

    def test_delete(self):
        self.list.insert("Jacob")
        self.list.insert("Pallymay")
        self.list.insert("Rasmus")

        # Delete the list head
        self.list.delete("Rasmus")
        self.assertTrue(self.list.head.get_data() == "Pallymay")

        # Delete the list tail
        self.list.delete("Jacob")
        self.assertTrue(self.list.head.get_next() is None)

    def test_delete_value_not_in_list(self):
        self.list.insert("Jacob")
        self.list.insert("Pallymay")
        self.list.insert("Rasmus")

        with self.assertRaises(ValueError):
            self.list.delete("Sunny")

    def test_delete_empty_list(self):
        with self.assertRaises(ValueError):
            self.list.delete("Sunny")

    def test_delete_next_reassignment(self):
        self.list.insert("Jacob")
        self.list.insert("Cid")
        self.list.insert("Pallymay")
        self.list.insert("Rasmus")

        self.list.delete("Pallymay")
        self.list.delete("Cid")

        self.assertTrue(self.list.head.next_node.get_data() == "Jacob")
示例#8
0
def test_insert_iterable():
    """test inserting iterable into linked list"""
    assert LL([1, 2, 3, 4])._size == 4
示例#9
0
def test_noniterable_as_argument():
    """Test raises correct error when passed noniterable."""
    with pytest.raises(TypeError) as err:
        LL(1234)
    assert str(err.value) == 'Please pass an iterable.'
def predefined_ll():
    """Default linked list for use with tests."""
    return LL([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
示例#11
0
def large_ll():
    """Long linked list."""
    return LL([1, 2, 3, 4, 5, 6, 7, 8])
def long_ll():
    """fixture for long array"""
    return LL([11, 12, 13, 14, 15, 16])
def empty_ll():
    """fixture for empty array"""
    return LL()
def short_ll():
    """fixture for short array"""
    return LL([5, 6, 7, 8])
def small_ll():
    """fixture for short array"""
    return LL([1, 2, 3, 4])
示例#16
0
 def setUp(self):
     self.list = LL()
 def __init__(self, max_size=1024):
     """Instantiates a Hash Table"""
     self.max_size = max_size
     self.buckets = [LL() for _ in range(self.max_size)]
def predefined_ll_other():
    """Second linked list for use with tests."""
    return LL([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
示例#19
0
def second_small_ll():
    """Small linked list."""
    return LL([1, 2, 3, 4])
def predefined_ll_short():
    """Shorter linked list for use with tests."""
    return LL([11, 12, 13, 14])
示例#21
0
def empty_ll():
    """Empty linked list."""
    return LL()
def empty_ll():
    """Empty linked list for use with tests."""
    return LL()
示例#23
0
def test_merge_none(predefined_ll):
    """Merge with empty lists."""
    empty = LL([])
    assert merge_lists(empty, predefined_ll).val == 1
    assert merge_lists(predefined_ll, empty).val == 1
    assert merge_lists(empty, empty) is None
def test_noniterable_as_argument():
    with pytest.raises(TypeError):
        LL(1234)
示例#25
0
def test_has_loop_has_none(one_four_ll):
    """
    test has loop with valid case of false
    """
    assert has_loop(LL([1, 2, 3, 4])) == False
示例#26
0
def linked_list_zip():
    return LL((9, 8, 7, 6))
示例#27
0
 def __init__(self, data):
     """init"""
     self.linked_list = LL(LLN(data))
def empty_ll():
    return LL()
示例#29
0
 def __init__(self, max_size=1024):
     self.max_size = max_size
     # self.buckets[LL()] * self.max_size
     self.buckets = [LL() for _ in range(self.max_size)]
示例#30
0
def test_kthFromEnd_emp():
    return LL()