def __init__(self, limit=10): self.max_nodes = limit self.num_nodes = 0 # values in cache_list are key/value dicts self.cache_list = DLL() # storage holds key, node pairs self.storage = {}
def test_dll_string(): test_dll = DLL() test_dll.add_tail(1) test_dll.add_tail(2) test_dll.add_tail("test") test_string = "[1, 2, test]" assert str(test_dll) == test_string
def test_dll_add(): test_dll = DLL() test_dll.add(1) assert test_dll.head.data == 1 assert test_dll.tail.data == 1 test_dll.add(2) assert test_dll.head.data == 1 assert test_dll.tail.data == 2
def test_query_position(): test_dll = DLL() test_dll.add_tail(1) test_dll.add_tail(2) test_dll.add_tail("test") assert test_dll.query_position(0) == 1 assert test_dll.query_position(1) == 2 assert test_dll.query_position(2) == "test"
def test_find_data(): test_dll = DLL() test_dll.add_tail(1) test_dll.add_tail(2) test_dll.add_tail("test") assert test_dll.query_position(0) == 1 assert test_dll.query_position(1) == 2 assert test_dll.query_position(2) == "test" assert test_dll.query_position(3) is None
def test_ddl_add_tail(): test_dll = DLL() test_dll.add_tail(1) assert test_dll.head.data == 1 assert test_dll.tail.data == 1 test_dll.add_tail(2) assert test_dll.head.data == 1 assert test_dll.tail.data == 2 test_dll.add_tail("test") assert test_dll.head.data == 1 assert test_dll.tail.data == "test"
def test_update_position(): test_dll = DLL() test_dll.add_tail(1) test_dll.add_tail(2) test_dll.add_tail("test") test_dll.update_position(0, 3) test_dll.update_position(1, 2) test_dll.update_position(2, 1) assert test_dll.query_position(0) == 3 assert test_dll.query_position(1) == 2 assert test_dll.query_position(2) == 1 assert test_dll.update_position(3, 1) == -1
def test_len(): test_dll = DLL() len_now = test_dll.len() assert len_now == 0 test_dll.add_tail(1) len_now = test_dll.len() assert len_now == 1 test_dll.add_tail(2) len_now = test_dll.len() assert len_now == 2 test_dll.add_tail("test") len_now = test_dll.len() assert len_now == 3
def test_remove_position(): test_dll = DLL() test_dll.add_tail(1) test_dll.add_tail(2) test_dll.add_tail("test") test_dll.remove_position(1) assert test_dll.head == 1 assert test_dll.tail == "test" test_dll.remove_position(0) assert test_dll.head == "test" assert test_dll.tail == "test" test_dll.remove_position(0) assert test_dll.head is None assert test_dll.tail is None
def test_remove_data(): test_dll = DLL() test_dll.add_tail(1) test_dll.add_tail(2) test_dll.add_tail("test") test_dll.remove_data(2) assert test_dll.head == 1 assert test_dll.tail == "test" test_dll.remove_data(1) assert test_dll.head == "test" assert test_dll.tail == "test" test_dll.remove_data("test") assert test_dll.head is None assert test_dll.tail is None
from doubly_linked_list import DoublyLinkedList as DLL dll = DLL() # initializing list for i in range(10): dll.add_to_tail(i) # def reverse_list node = dll.head prev = None while node: next = node.next node.prev = node.next node.next = prev prev = node node = next temp_head = dll.head dll.head = dll.tail dll.tail = temp_head # print list node = dll.head while node: print(node.value) node = node.next def reverse_list(dll): node = dll.head
def __init__(self, node=None): self._list = DLL() if node != None: self.push(node)
def __init__(self, node=None): self._list = DLL() if node != None: self.enqueue(node)
def generate_DLL(): data, n = DLL(), randint(10, 20) for _ in range(n): r = randint(1, 100) data.insert_into_tail(r) return data
def setUp(self): self.list = DLL()
class TestDoublyLinkedLists(unittest.TestCase): def setUp(self): self.list = DLL() def test_node_initialization(self): """"test initialization of Node""" n = Node(10) self.assertEqual(n.val, 10) def test_linked_list_initialization(self): """"test initialization of DLL""" self.assertIsNone(self.list.head) self.assertIsNone(self.list.tail) def test_push(self): self.assertEqual(self.list.length, 0) self.list.push(5) self.assertEqual(self.list.length, 1) self.list.push(7).push(10) self.assertEqual(self.list.length, 3) def test_pop(self): self.assertIsNone(self.list.pop()) self.list.push(4).push(5) self.assertEqual(self.list.length, 2) self.assertEqual(self.list.pop(), 5) self.assertEqual(self.list.length, 1) def test_unshift(self): self.list.push(4) self.list.unshift(1) self.assertEqual(self.list.length, 2) self.assertEqual(self.list.pop(), 4) self.assertEqual(self.list.pop(), 1) self.assertEqual(self.list.length, 0) def test_shift(self): self.list.push(4).push(2) self.assertEqual(self.list.shift(), 4) self.assertEqual(self.list.length, 1) self.assertEqual(self.list.shift(), 2) self.assertEqual(self.list.length, 0) def test_get(self): self.list.push(0).push(1).push(2).push(3).push(4) self.assertEqual(self.list.length, 5) self.assertEqual(self.list.get(2), 2) self.assertEqual(self.list.get(0), 0) self.assertIsNone(self.list.get(5)) self.assertEqual(self.list.get(4), 4) self.assertEqual(self.list.get(3), 3) def test_set(self): self.list.push(0).push(1).push(2).push(3).push(4) self.assertEqual(self.list.length, 5) self.list.set(4, 99) self.assertEqual(self.list.pop(), 99) self.assertEqual(self.list.length, 4) self.list.set(0, -99) self.assertEqual(self.list.shift(), -99) self.assertEqual(self.list.length, 3) def test_remove_first_element(self): self.list.push(1) self.assertEqual(self.list.length, 1) self.assertEqual(self.list.remove(0), 1) self.assertEqual(self.list.length, 0) def test_remove_last_element(self): self.list.push(1).push(2).push(3).push(4).push(5) self.assertEqual(self.list.length, 5) self.assertEqual(self.list.remove(4), 5) self.assertEqual(self.list.length, 4) def test_remove_second_element(self): self.list.push(1).push(2).push(3).push(4).push(5) self.assertEqual(self.list.length, 5) self.assertEqual(self.list.remove(1), 2) self.assertEqual(self.list.length, 4) def test_remove_second_to_last_element(self): self.list.push(1).push(2).push(3).push(4).push(5) self.assertEqual(self.list.length, 5) self.assertEqual(self.list.remove(3), 4) self.assertEqual(self.list.length, 4) def test_insert(self): """write your own tests!""" def test_reverse(self): """write your own tests!"""