コード例 #1
0
def test_delete_node_method_reassigns_next_node_to_none_if_deleting_end_Node():
    """Test that the delete_node method of the Singly Linked List correctly
    reassigns the next_node to Node if it's removing the last Node of the list
    ."""

    ll = SinglyLinkedList()
    ll.add_node(1)
    ll.add_node(2)

    ll.delete_node(1)
    assert ll.head.next_node is None
コード例 #2
0
def test_delete_node_method_reassigns_head_of_list_if_head_is_deleted():
    """Test that the delete_node method of the Singly Linked List class
    correctly reassigns the head Node of the list to the next Node if the head
    Node is removed."""

    ll = SinglyLinkedList()
    ll.add_node(1)
    ll.add_node(2)

    ll.delete_node(2)
    assert ll.head.data == 1
コード例 #3
0
def test_linked_list_insert_middle_index():
    new_list = SinglyLinkedList()
    new_list.addAtHead(4)
    new_list.addAtTail(6)

    new_list.addAtIndex(1, 5)
    assert new_list.head.val == 4
    assert new_list.tail.val == 6
    assert new_list.head.next.val == 5
    assert new_list.head.next.next.val == 6
    assert new_list.length == 3
 def test_add_to_head(self):
     """
     Tests that a new Node has been added to the
     head of the list.
     """
     sll = SinglyLinkedList()
     sll.add_to_head(1)
     sll.add_to_head(2)
     self.assertEqual(sll.head.value, 2)
     sll.add_to_head(3)
     self.assertEqual(sll.head.value, 3)
     self.assertEqual(len(sll), 3)
コード例 #5
0
def test_singly_linked_list_remove_end(N):
    print("Testing SinglyLinkedList")
    l = SinglyLinkedList()

    print(f"Prepending {int(N)} values")
    [l.prepend(i) for i in range(int(N))]
    print(f"Removing {int(N)} values from the end")
    [l.remove_end() for i in range(int(N))]
    try:
        l.remove_end()
    except Exception:
        print("Got expected Exception")
 def test_remove_from_head(self):
     """
     Tests that Node has been removed from the
     head of the list.
     """
     sll = SinglyLinkedList()
     sll.add_to_head(1)
     sll.add_to_head(2)
     self.assertEqual(sll.head.value, 2)
     sll.remove_from_head()
     self.assertEqual(sll.head.value, 1)
     self.assertEqual(len(sll), 1)
コード例 #7
0
def test_delete_node_method_correctly_reassigns_pointers_if_deleting_middle():
    """Test that the delete_node method of the Singly Linked List class
    correctly reassigns pointers if deleting a Node that has two Nodes on
    either side of it."""

    ll = SinglyLinkedList()

    for i in range(3):
        ll.add_node(i)

    ll.delete_node(1)
    assert ll.head.data == 2 and ll.head.next_node.data == 0
コード例 #8
0
    def test_kth_to_last(self):

        # Positive tests.

        lst = SinglyLinkedList()
        n = 4
        for i in range(n):
            lst.insert_tail(Node(i))
        for k in range(n):
            self.assertEqual(kth_to_last(lst, k).data, n - k - 1)

        # Negative tests.

        lst = SinglyLinkedList()
        n = 4
        for i in range(n):
            lst.insert_tail(Node(i))
        k = -1
        self.assertEqual(kth_to_last(lst, k), None)
        k = n
        self.assertEqual(kth_to_last(lst, k), None)
 def test_get_max(self):
     """
     Tests that the maximum value in the list is
     returned.
     """
     sll = SinglyLinkedList()
     sll.add_to_head(3)
     sll.add_to_tail(9)
     sll.add_to_head(10)
     sll.add_to_head(4)
     max_val = sll.get_max()
     self.assertEqual(max_val, 10)
コード例 #10
0
def main():
    """Provides access to the module as standalone project."""
    sll = SinglyLinkedList()
    dll = DoublyLinkedList()

    dll.insert_head(10)
    dll.insert_head(11)
    dll.insert_tail(5)
    dll.insert_head(15)
    dll.insert_tail(200)
    dll.insert(200, 4)
    dll.print()
 def test_move_to_front(self):
     """
     Tests that a Node has been moved to the front
     of the list.
     """
     sll = SinglyLinkedList()
     sll.add_to_head(1)
     sll.add_to_head("testing")
     node = sll.head
     sll.add_to_head(5)
     sll.move_to_front(node)
     self.assertEqual(sll.head.value, "testing")
     self.assertEqual(len(sll), 3)
コード例 #12
0
def test_linked_list_delete_middle():
    new_list = SinglyLinkedList()
    new_list.addAtHead(7)
    new_list.addAtHead(2)
    new_list.addAtHead(1)
    new_list.addAtIndex(3, 0)
    new_list.deleteAtIndex(2)
    new_list.addAtHead(6)
    new_list.addAtTail(4)

    assert new_list.head.val == 6
    assert new_list.tail.val == 4
    assert new_list.length == 4
    assert new_list.get(4) == 4
コード例 #13
0
    def test_remove_first(self):
        llist = SinglyLinkedList()
        self.assertRaises(Exception, llist.remove_first)

        llist.insert_last(0)
        llist.insert_last(1)
        llist.insert_last(2)
        self.assertEqual(llist.remove_first(), 0)
        self.assertEqual(llist.size, 2)
        self.assertEqual(llist.head.data, 1)
        self.assertEqual(llist.remove_first(), 1)
        self.assertEqual(llist.size, 1)
        self.assertEqual(llist.head.data, 2)
        self.assertEqual(llist.remove_first(), 2)
        self.assertEqual(llist.size, 0)
        self.assertEqual(llist.head, None)
コード例 #14
0
    def test_insert_at(self):
        llist = SinglyLinkedList()
        self.assertRaises(Exception, llist.insert_at, 1, 0)

        llist.insert_at(0, 0)
        self.assertEqual(llist.size, 1)
        self.assertEqual(llist.head.data, 0)

        llist.insert_at(0, -1)
        self.assertEqual(llist.size, 2)
        self.assertEqual(llist.head.data, -1)
        self.assertEqual(llist.head.next.data, 0)

        llist.insert_at(2, 1)
        self.assertEqual(llist.size, 3)
        self.assertEqual(llist.head.data, -1)
        self.assertEqual(llist.head.next.data, 0)
        self.assertEqual(llist.head.next.next.data, 1)
    def test_add_to_tail(self):
        """
        Tests that a new Node has been added to
        the tail of the list.
        """
        def get_tail_val(list):
            currentNode = list.head
            while currentNode.next:
                currentNode = currentNode.next
            return currentNode.value

        sll = SinglyLinkedList()
        sll.add_to_tail(1)
        sll.add_to_tail(2)
        self.assertEqual(get_tail_val(sll), 2)
        sll.add_to_tail(90)
        self.assertEqual(get_tail_val(sll), 90)
        self.assertEqual(len(sll), 3)
コード例 #16
0
    def test_exists(self):
        """ Tests for membership.
        """
        sll = SinglyLinkedList()
        node1, node2, node3 = Node(1), Node(2), Node(3)

        sll.append(node1)
        sll.append(node2)
        sll.append(node3)

        self.assertTrue(sll.exists(node1))
        self.assertTrue(sll.exists(node2))
        self.assertTrue(sll.exists(node3))

        self.assertFalse(sll.exists(Node(4)))

        # Membership is based on object identity, not value
        self.assertFalse(sll.exists(Node(1)))
コード例 #17
0
    def test_removals(self):
        """ Test list removals.
        """
        node1 = Node('first')
        sll = SinglyLinkedList(node1)

        node2 = Node('second')
        sll.insert_after(node1, node2)

        self.assertEquals(str(sll), 'first, second')

        sll.remove_after(node1)

        self.assertEquals(str(sll), 'first')

        sll.insert_after(node1, node2)

        sll.remove_beginning()

        self.assertEquals(str(sll), 'second')
コード例 #18
0
    def test_delete(self):
        """ Test a delete method.
        """
        sll = SinglyLinkedList()
        node1, node2, node3 = Node(1), Node(2), Node(3)

        sll.append(node1)
        sll.append(node2)
        sll.append(node3)

        self.assertEquals(str(sll), '1, 2, 3')

        sll.delete(node2)

        self.assertEquals(str(sll), '1, 3')

        # It silently handles missing items
        sll.delete(Node(4))

        self.assertEquals(str(sll), '1, 3')
コード例 #19
0
    def test_inserts(self):
        """ Test list inserts.
        """
        sll = SinglyLinkedList(Node('first'))

        sll.insert_after(sll.first_node, Node('second'))

        self.assertEquals(sll.first_node.data, 'first')

        self.assertEquals(sll.first_node.next_node.data, 'second')

        sll.insert_beginning(Node('before the first'))

        self.assertEquals(sll.first_node.data, 'before the first')

        self.assertEquals(sll.first_node.next_node.data, 'first')

        self.assertEquals(sll.first_node.next_node.next_node.data, 'second')

        self.assertEquals(str(sll), 'before the first, first, second')
    def test_move_to_end(self):
        """
        Tests that a Node has been moved to the end of
        the list.
        """
        def get_tail_val(list):
            currentNode = list.head
            while currentNode.next:
                currentNode = currentNode.next
            return currentNode.value

        sll = SinglyLinkedList()
        sll.add_to_head(1)
        sll.add_to_head(2)
        sll.add_to_head("testing")
        node = sll.head
        sll.add_to_head(3)
        sll.move_to_end(node)
        self.assertEqual(get_tail_val(sll), "testing")
        self.assertEqual(len(sll), 4)
コード例 #21
0
    def test_remove(self):
        llist = SinglyLinkedList()
        self.assertRaises(Exception, llist.remove)

        llist.insert_last(0)
        llist.insert_last(1)
        llist.insert_last(2)
        llist.insert_last(3)
        self.assertEqual(llist.remove(0), 0)
        self.assertEqual(llist.size, 3)
        self.assertEqual(llist.head.data, 1)
        self.assertEqual(llist.remove(2), 2)
        self.assertEqual(llist.size, 2)
        self.assertEqual(llist.head.data, 1)
        self.assertEqual(llist.head.next.data, 3)
        self.assertEqual(llist.remove(3), 3)
        self.assertEqual(llist.size, 1)
        self.assertEqual(llist.head.data, 1)
        self.assertEqual(llist.head.next, None)
        self.assertEqual(llist.remove(1), 1)
        self.assertEqual(llist.size, 0)
        self.assertEqual(llist.head, None)
コード例 #22
0
def split(linked_list):
    """
    Divide the unsorted list at midpoint into sublists
    Takes O(k log n) time
    """

    if linked_list == None or linked_list.head == None:
        left_half = linked_list
        right_half = None

        return left_half, right_half
    else:
        size = linked_list.size()
        mid = size // 2

        mid_node = linked_list.node_at_index(mid - 1)

        left_half = linked_list
        right_half = SinglyLinkedList()
        right_half.head = mid_node.next_node
        mid_node.next_node = None

        return left_half, right_half
コード例 #23
0
 def __init__(self):
     self.S = SinglyLinkedList()
def convert_array_to_singly_linked_list(elems: list[any]) -> SinglyLinkedList:
    res = SinglyLinkedList()
    for elem in elems:
        res.insert_tail(elem)
    return res
コード例 #25
0
 def __init__(self):
     self.size = 0
     self.storage = SinglyLinkedList()
コード例 #26
0
 def __init__(self, capacity=10):
     """ Initialize scoreboard with given maximum capacity."""
     self._board = SinglyLinkedList()
     self._cap = capacity
コード例 #27
0
    def initialize_graph(self):

        for vertex in self.vertices:
            self.adjacencyList[vertex] = SinglyLinkedList()
コード例 #28
0
 def __init__(self):
     self.__linked_list = SinglyLinkedList()
     self.length = 0
コード例 #29
0
    def test_remove_dups(self):

        # Check removing dups from the empty list.
        lst = SinglyLinkedList()
        remove_dups(lst)
        self.assertEqual(lst.size, 0)

        # Check removing dups from the list wiht one element in it.
        lst = SinglyLinkedList()
        lst.insert_tail(Node(1))
        remove_dups(lst)
        self.assertEqual(lst.size, 1)
        self.assertEqual(lst.head.data, 1)

        # Check removing dups from the tree with repeated element in it.
        lst = SinglyLinkedList()
        original_data = [1, 1]
        for i in range(len(original_data)):
            lst.insert_tail(Node(original_data[i]))
        remove_dups(lst)
        self.assertEqual(lst.size, 1)
        self.assertEqual(lst.head.data, 1)

        lst = SinglyLinkedList()
        original_data = [1, 1, 1, 1, 1]
        for i in range(len(original_data)):
            lst.insert_tail(Node(original_data[i]))
        remove_dups(lst)
        self.assertEqual(lst.size, 1)
        self.assertEqual(lst.head.data, 1)

        # Check removing dups from the tree with mixed data.
        lst = SinglyLinkedList()
        original_data = [1, 5, 3, 5, 4, 8, 1, 12, 33, 5]
        expected_data = [1, 5, 3, 4, 8, 12, 33]
        for i in range(len(original_data)):
            lst.insert_tail(Node(original_data[i]))
        remove_dups(lst)
        self.assertEqual(lst.size, len(expected_data))
        node = lst.head
        for i in range(len(expected_data)):
            self.assertEqual(node.data, expected_data[i])
            node = node.next

        lst = SinglyLinkedList()
        original_data = [38, 49, 51, 1080, 12, -48, -48, 1080]
        expected_data = [38, 49, 51, 1080, 12, -48]
        for i in range(len(original_data)):
            lst.insert_tail(Node(original_data[i]))
        remove_dups(lst)
        self.assertEqual(lst.size, len(expected_data))
        node = lst.head
        for i in range(len(expected_data)):
            self.assertEqual(node.data, expected_data[i])
            node = node.next

        # Check removing dups from the tree with no repeated data.
        lst = SinglyLinkedList()
        original_data = [1, 2, 3, 4, 5]
        expected_data = [1, 2, 3, 4, 5]
        for i in range(len(original_data)):
            lst.insert_tail(Node(original_data[i]))
        remove_dups(lst)
        self.assertEqual(lst.size, len(expected_data))
        node = lst.head
        for i in range(len(expected_data)):
            self.assertEqual(node.data, expected_data[i])
            node = node.next
コード例 #30
0
from singly_linked_list import SinglyLinkedList
from collections import deque
from queue import Queue

if __name__ == '__main__':
    print(
        'Queue implementation using Singly Linked List (TC add_tail() - O(1), TC remove_head() - O(1))'
    )
    test_queue = SinglyLinkedList()
    print(test_queue)
    test_queue.add_tail(1)
    test_queue.add_tail(2)
    test_queue.add_tail(3)
    print(test_queue)
    test_queue.remove_head()
    print(test_queue)
    test_queue.remove_head()
    test_queue.remove_head()
    print(test_queue)

    print()
    print(
        'Queue implementation using collections.deque (TC append() - O(1), TC popleft() - O(1))'
    )
    test_queue = deque()
    print(test_queue)
    test_queue.append(1)
    test_queue.append(2)
    test_queue.append(3)
    print(test_queue)
    test_queue.popleft()