Exemplo n.º 1
0
class ListQueue:
    # TODO limit size
    def __init__(self, size=0):
        self.list_obj = EndedLinkedList()

# top of the stack is

    def list(self):
        return [link.key for link in self.list_obj.array()]

    def isEmpty(self):
        return self.list_obj.isEmpty()

    def insert(self, key):
        self.list_obj.insertLast(key, None)

    def remove(self):
        if self.isEmpty():
            raise ValueError("Removing from empty queue")
        return self.list_obj.deleteFirst().key

    def head(self):
        if self.isEmpty():
            raise ValueError("Reading head from empty queue")
        return self.list_obj.first.key
Exemplo n.º 2
0
 def test_delete_non_existing(self):
     list = EndedLinkedList()
     list.insertFirst(1, {"a": 1})
     list.insertFirst(2, {"b": 2})
     list.insertFirst(3, {"c": 3})
     self.assertEqual(list.delete(2).data["b"], 2)
     self.assertEqual(len(list.array()), 2)
Exemplo n.º 3
0
 def test_delete_existing(self):
     list = EndedLinkedList()
     list.insertFirst(1, {"a": 1})
     list.insertFirst(2, {"b": 2})
     list.insertFirst(3, {"c": 3})
     self.assertEqual(list.delete(4), None)
     self.assertEqual(len(list.array()), 3)
Exemplo n.º 4
0
 def test_insert_last(self):
     list = EndedLinkedList()
     list.insertFirst(1, {"a": 1})
     list.insertFirst(2, {"b": 2})
     list.insertLast(3, {"c": 3})
     self.assertEqual(list.last.key, 3)
     self.assertEqual(list.first.next.next.key, 3)
Exemplo n.º 5
0
 def test_insert_before_first(self):
     list = EndedLinkedList()
     list.insertFirst(1, {"a": 1})
     list.insertFirst(2, {"b": 2})
     list.insertBefore(2, 3, {"c": 3})
     self.assertEqual(list.first.key, 3)
     self.assertEqual(list.first.data["c"], 3)
     self.assertEqual(list.first.next.key, 2)
Exemplo n.º 6
0
 def test_delete_first_non_empty(self):
     list = EndedLinkedList()
     list.insertFirst(1, {"a": 1})
     list.insertFirst(2, {"b": 2})
     self.assertEqual(list.deleteFirst().data["b"], 2)
     self.assertEqual(len(list.array()), 1)
Exemplo n.º 7
0
 def test_delete_first_empty(self):
     list = EndedLinkedList()
     self.assertEqual(list.deleteFirst(), None)
Exemplo n.º 8
0
 def test_delete_from_empty(self):
     list = EndedLinkedList()
     self.assertEqual(list.delete(4), None)
Exemplo n.º 9
0
 def test_find_non_existing(self):
     list = EndedLinkedList()
     list.insertFirst(1, {"a": 1})
     list.insertFirst(2, {"b": 2})
     list.insertFirst(3, {"c": 3})
     self.assertEqual(list.find(4), None)
Exemplo n.º 10
0
 def test_find_existing(self):
     list = EndedLinkedList()
     list.insertFirst(1, {"a": 1})
     list.insertFirst(2, {"b": 2})
     list.insertFirst(3, {"c": 3})
     self.assertEqual(list.find(2).data["b"], 2)
Exemplo n.º 11
0
 def test_insert_first(self):
     list = EndedLinkedList()
     list.insertFirst(1, {"a": 1})
     list.insertFirst(2, {"b": 2})
     self.assertEqual(list.first.key, 2)
     self.assertEqual(list.first.data["b"], 2)
Exemplo n.º 12
0
 def test_insert_last_to_empty(self):
     list = EndedLinkedList()
     list.insertLast(3, {"c": 3})
     self.assertEqual(list.last.key, 3)
     self.assertEqual(list.first.key, 3)
Exemplo n.º 13
0
 def __init__(self, size=0):
     self.list_obj = EndedLinkedList()