def test_first_item(self): test_list = DoublyLinkedList() test_list.insert_front(5) test_list.insert_front(9) pop = test_list.pop_front() self.assertEqual(9, pop, "first item did not insert correctly")
def test_container(): linked_list = DoublyLinkedList() linked_list.add_last(1) linked_list.add_last(2) assert 1 in linked_list assert 2 in linked_list assert 3 not in linked_list
class LruCache: def __init__(self, capacity): self._cache = {} # dictionary to contain key-value self._doubly_linked_list = DoublyLinkedList() self._capacity = capacity self._size = 0 def get(self, key): if key not in self._cache: return -1 else: node = self._cache[key] self._doubly_linked_list.move_to_end(node) return node.val def set(self, key, value): if self._size == self._capacity: # Cache is full delete least recently used node self._cache.pop(self._doubly_linked_list.get_start_node().key) self._doubly_linked_list.delete_from_beginning() self._size = self._size - 1 if key in self._cache: # Key already exists, need to delete it from doubly linked list first self._doubly_linked_list.delete(self._cache[key]) self._size = self._size - 1 node = Node(key, value) self._cache[key] = node self._doubly_linked_list.add_to_end(node) self._size = self._size + 1 def print(self): for k in self._cache: print('{} = {}'.format(k, self._cache[k].val))
def test_remove_added_last(method_name, size, head, tail): dll = DoublyLinkedList() for n in range(size): dll.add_last(n) method = getattr(dll, method_name) method() assert len(dll) == (size - 1) assert dll.head == head assert dll.tail == tail
class PriorityQueue: def __init__(self): self._list = DoublyLinkedList() def __len__(self) -> int: return len(self._list) def __iter__(self) -> Generator[T]: yield from self._list def enqueue(self, data: T) -> None: if len(self) == 0: self._list.add_last(data) else: current = self._list.head while current and current.data > data: current = current.next_ if current is None: self._list.add_last(data) else: self._list.add_before(current, data) def peek(self) -> T: if len(self) == 0: raise IndexError("peek from empty priority queue") return self._list.head.data def dequeue(self) -> T: if len(self) == 0: raise IndexError("dequeue from empty priority queue") data = self.peek() self._list.remove_first() return data
def test_add_three_items( method_name, expected_head_value, expected_middle_value, expected_tail_value, ): linked_list = DoublyLinkedList() method = getattr(linked_list, method_name) method(1) method(2) method(3) assert len(linked_list) == 3 head = linked_list.head middle = head.next_ tail = linked_list.tail assert head == expected_head_value assert head.previous is None assert head.next_ == middle assert middle == expected_middle_value assert middle.previous == head assert middle.next_ == tail assert tail == expected_tail_value assert tail.previous == middle assert tail.next_ is None
class QueueLinkedList(Generic[T]): """ Queue backed by a doubly linked list """ def __init__(self): self.linked_list = DoublyLinkedList() def __len__(self): return self.linked_list.size() # O(1) def dequeue(self) -> T: return self.linked_list.remove(0) # O(1) def enqueue(self, element: T): self.linked_list.append(element)
class Stack: def __init__(self): self.stack = DoublyLinkedList() def push(self, data): self.stack.add_node(data) @property def pop(self): self.stack.remove_node() @property def empty(self): self.stack.lenght = 0 @property def peek(self): return self.stack.show(reverse=True)
def test_pop_from_front(self): test_list = DoublyLinkedList() test_list.insert_front(5) test_list.insert_front(7) test_list.insert_front(9) front_pop = test_list.pop_front() node = test_list.pop_front() self.assertEqual( 7, node._node_value, "First item isn't the second item before pop from front")
def test_size(): linked_list = DoublyLinkedList() assert linked_list.size() == 0 linked_list.append(1) assert linked_list.size() == 1 assert get_multi_node_list().size() == 4
def test_append(): doubly_linked_list = DoublyLinkedList() doubly_linked_list.append(3) doubly_linked_list.append(4) assert doubly_linked_list.size() == 2 assert doubly_linked_list.get(0) == 3 assert doubly_linked_list.get(1) == 4
def get_multi_node_list() -> DoublyLinkedList: multi_node_list = DoublyLinkedList() multi_node_list.append(5) multi_node_list.append(2) multi_node_list.append(4) multi_node_list.append(1) return multi_node_list
def test_pop_front(self): #Arrange double_linked = DoublyLinkedList() double_linked.insert_front(1) double_linked.insert_front(2) double_linked.insert_front(3) #Act double_linked.pop_front() #Assert self.assertEqual(2, double_linked.first_node.value, "pop_front did not return the correct node value") self.assertEqual(None, double_linked.first_node.prev_node, "pop_front did not return the correct node value")
def test_iteration(): linked_list = DoublyLinkedList() linked_list.add_last(1) linked_list.add_last(2) linked_list.add_last(3) values_list = [item for item in linked_list] assert values_list == [1, 2, 3]
def test_reversed(): linked_list = DoublyLinkedList() linked_list.add_last(1) linked_list.add_last(2) linked_list.add_last(3) values_list = [item for item in reversed(linked_list)] assert values_list == [3, 2, 1]
def test_add_one_item(method_name): linked_list = DoublyLinkedList() method = getattr(linked_list, method_name) method(1) assert len(linked_list) == 1 head = linked_list.head tail = linked_list.tail assert head == 1 assert head.previous is None assert head.next_ is None assert tail == 1 assert tail.previous is None assert tail.next_ is None
def test_insert_back(self): #Arrange double_linked = DoublyLinkedList() #Act double_linked.insert_back(3) double_linked.insert_back(2) double_linked.insert_back(1) #Assert self.assertEqual(3, double_linked.first_node.value, "The value returned is not correct")
def test_pop_back(self): #Arrange double_linked = DoublyLinkedList() double_linked.insert_front(1) double_linked.insert_front(2) #Act double_linked.pop_back() #Assert self.assertEqual(None, double_linked.first_node.next_node, "pop_back did not return correct node value")
def test_add_two_items(method_name, expected_head_value, expected_tail_value): linked_list = DoublyLinkedList() method = getattr(linked_list, method_name) method(1) method(2) assert len(linked_list) == 2 head = linked_list.head tail = linked_list.tail assert head == expected_head_value assert head.previous is None assert head.next_ == linked_list.tail assert tail == expected_tail_value assert tail.previous == head assert tail.next_ is None
def test_insert_front(self): #Arrange double_linked = DoublyLinkedList() #Act double_linked.insert_front(1) double_linked.insert_front(2) double_linked.insert_front(3) #Assert self.assertEqual(3, double_linked.first_node.value, "The value returned is not correct") self.assertEqual(None, double_linked.first_node.prev_node, "The value returned is not correct")
class Queue: def __init__(self): self.queue = DoublyLinkedList() def add(self, data): self.queue.add_node(data) @property def remove(self): self.queue.remove_node(1) @property def empty(self): self.queue.lenght = 0 @property def peek(self): self.queue.show()
def test_add_before_tail(): linked_list = DoublyLinkedList() linked_list.add_last(1) linked_list.add_last(3) linked_list.add_before(linked_list.tail, 2) head = linked_list.head middle = head.next_ tail = linked_list.tail assert head == 1 assert head.previous is None assert head.next_ == middle assert middle == 2 assert middle.previous == head assert middle.next_ == tail assert tail == 3 assert tail.previous == middle assert tail.next_ is None
def setUp(self): self.items = DoublyLinkedList()
class TestDoublyLinkedList(unittest.TestCase): def setUp(self): self.items = DoublyLinkedList() # test for empty linked list def test_empty_list(self): self.assertEqual(self.items.head, None) self.assertEqual(self.items.tail, None) # Tests forbidden operations like removal in a empty linked list def test_forbidden_operations_on_empty_linked_list(self): self.assertEqual(self.items.size, 0) with self.assertRaises(RuntimeError): self.items.remove_back() with self.assertRaises(RuntimeError): self.items.remove_front() def test_delete_nonexistent_item(self): self.items.insert_back('A') self.items.insert_front('B') with self.assertRaises(RuntimeError): self.items.remove('Z') def test_insert_back(self): self.items.insert_back('Z') self.assertEqual(self.items.size, 1) self.assertEqual(self.items.remove_back(), 'Z') self.items.insert_back('Y') self.items.insert_back('T') self.items.insert_back('E') self.assertEqual(self.items.remove_front(), 'Y') def test_insert_front(self): self.items.insert_front('A') self.assertEqual(self.items.size, 1) self.assertEqual(self.items.remove_front(), 'A') self.items.insert_front('B') self.items.insert_front('C') self.items.insert_front('D') self.assertEqual(self.items.remove_back(), "B") self.assertEqual(self.items.remove_front(), "D") def test_remove(self): with self.assertRaises(RuntimeError): self.items.remove('T') self.items.insert_front('T') self.items.insert_back('R') self.items.insert_after('R', 'Y') self.items.remove('R') self.assertEqual(self.items.size, 2) self.assertEqual(self.items.remove_back(), 'Y') self.assertEqual(self.items.size, 1) self.items.remove_front() self.assertEqual(self.items.size, 0) with self.assertRaises(RuntimeError): self.items.remove_front() # when only one node is there def test_single_node_case(self): self.items.insert_front(1) self.items.remove_front() self.assertEqual(self.items.head, self.items.tail)
def test_insert_empty(): doubly_linked_list = DoublyLinkedList() doubly_linked_list.insert(3, 0) assert doubly_linked_list.size() == 1 assert doubly_linked_list.get(0) == 3
def test_remove_empty(): doubly_linked_list = DoublyLinkedList() assert not doubly_linked_list.remove(0)
def __init__(self): self.queue = DoublyLinkedList()
def __init__(self, capacity): self._cache = {} # dictionary to contain key-value self._doubly_linked_list = DoublyLinkedList() self._capacity = capacity self._size = 0
def __init__(self): self.linked_list = DoublyLinkedList()
from data_structures.doubly_linked_list import DoublyLinkedList # creating a doubly linked list animals = DoublyLinkedList() print('Adding a few elements on the front...') animals.insert_front('Cow') animals.insert_front('Tiger') animals.display() print('\nAdding a few elements on the back...') animals.insert_back('Lion') animals.insert_back('Wolf') animals.display() print('\nAdding an item after Lion') animals.insert_after('Lion', 'Lioness') animals.display() print('\nRemoving a few items from beginning..') animals.remove_front() animals.remove_front() animals.display() print('\nRemoving Lioness from the list') animals.remove('Lioness') animals.display() print('\nRemoving a few items from the back') animals.remove_back() animals.remove_back()