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)
class Stack:
    def __init__(self):
        self.s = LinkedList()
        self.head = self.s.sentinel

    def push(self, x):
        newnode = Node(x)
        self.s.insert(newnode)

    def pop(self):
        x = self.s.head
        if x is None:
            raise UnderFlowError
        self.s.delete(self.s.head)
        return x
Esempio n. 3
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')
Esempio n. 4
0
class Queue:
    """
        Queue will be implemented with a linked list as the start
        of the queue as the tail, and the end of the queue as the head.
        This way, enqueue's and dequeue's are O(1).

        Enqueue is appending from the tail, dequeueing is reassigning
        the pointer of the head to None.
    """
    def __init__(self, iterable=None):
        self.queue = SinglyLinkedList()

        if iterable:
            for item in iterable:
                self.queue.append(item)

    def is_empty(self):
        return self.queue.is_empty()

    def length(self):
        return self.queue.size

    def front(self):
        if self.queue.is_empty():
            return None
        return self.queue.head.data

    def enqueue(self, data):
        self.queue.append(data)

    def dequeue(self):
        if self.queue.size < 1:
            raise ValueError("queue is empty")

        output = self.front()

        self.queue.delete(output)

        return output