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
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)
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)
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)
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)
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)
def test_delete_first_empty(self): list = EndedLinkedList() self.assertEqual(list.deleteFirst(), None)
def test_delete_from_empty(self): list = EndedLinkedList() self.assertEqual(list.delete(4), None)
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)
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)
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)
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)
def __init__(self, size=0): self.list_obj = EndedLinkedList()