def test_length(self):
     """
     Tests that the correct length of the list is
     returned.
     """
     sll = SinglyLinkedList()
     sll.add_to_head(1)
     sll.add_to_head(2)
     sll.add_to_head(3)
     length = len(sll)
     self.assertEqual(length, 3)
 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)
 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)
コード例 #4
0
class Stack:
    def __init__(self):
        self.size = 0
        # Why is our DLL a good choice to store our elements?
        self.storage = SinglyLinkedList()

    def push(self, value):
        self.storage.add_to_head(value)
        self.size += 1

    def pop(self):
        if self.size:
            self.size -= 1
            return self.storage.remove_from_head()
        return None

    def len(self):
        return self.size
    # pass
コード例 #5
0
class Stack:
    def __init__(self):
        self.size = 0
        self.storage = SinglyLinkedList()

    def __len__(self):
        return self.size

    def push(self, value):
        # increase the size of the stack by one
        self.size += 1
        # add an element to the front of the linked list
        self.storage.add_to_head(value)

    def pop(self):
        # check if empty
        if self.size == 0:
            return None
        # decrement the size of the stack by one
        self.size -= 1
        # remove the first element in storage and return the removed head
        node = self.storage.remove_head()
        return node
 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)
 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)
    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)
    def test_delete(self):
        """
        Tests that a Node has been deleted from the
        list.
        """
        def search_for_val(list, val):
            currentNode = list.head
            found_val = None
            while currentNode.next:
                if currentNode.value == val:
                    found_val = currentNode.value
                currentNode = currentNode.next
            return found_val

        sll = SinglyLinkedList()
        sll.add_to_head(1)
        sll.add_to_head(2)
        sll.add_to_head(3)
        sll.add_to_head(4)
        sll.add_to_head(5)
        node = sll.head
        sll.add_to_head(6)
        sll.add_to_head(7)
        sll.add_to_head(8)
        sll.add_to_head(9)
        sll.add_to_head(10)
        sll.delete(node)
        self.assertEqual(search_for_val(sll, node.value), None)
    def test_remove_from_tail(self):
        """
        Tests that a Node has been removed from 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_head(1)
        sll.add_to_head(2)
        sll.add_to_head(3)
        sll.add_to_head(4)
        sll.add_to_head(5)
        sll.add_to_head(6)
        sll.add_to_head(7)
        sll.add_to_head(8)
        sll.add_to_head(9)
        sll.add_to_head(10)
        sll.remove_from_tail()
        self.assertEqual(get_tail_val(sll), 2)
        self.assertEqual(len(sll), 9)