def main(): numbers1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] lst1 = DoublyList() common = None for idx, n in enumerate(numbers1): node = Node(n, "hello:%d" % n) lst1.add_node(node) if idx == 5: common = node lst2 = DoublyList() numbers2 = [13, 14, 15] for idx, n in enumerate(numbers2): lst2.add(n, "hello:%d" % n) # join them lst2.tail.next = common lst2.tail = lst1.tail assert common == common_node(lst1, lst2) numbers1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] lst1 = DoublyList() for idx, n in enumerate(numbers1): node = Node(n, "hello:%d" % n) lst1.add_node(node) lst2 = DoublyList() numbers2 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] for idx, n in enumerate(numbers2): lst2.add(n, "hello:%d" % n) assert None is common_node(lst1, lst2)
def test_insert_before(self): """ Tests that a Node can add another Node before it """ node = Node(1) node.insert_before(2) self.assertEqual(2, node.prev.value)
def insert_at_beginning(head, data): temp = head new_node = Node(data) new_node.next = temp temp.prev = new_node head = new_node return head
def test_insert_after(self): """ Tests that a Node can add another Node after it """ node = Node(1) node.insert_after(2) self.assertEqual(2, node.next.value)
def clone_list(lst): # step 1 # append new nodes after existing nodes current = lst.head while current: current_next = current.next new_node = Node(current.key, current.data + " :: new") current.next = new_node new_node.next = current_next current = current_next # step2 # assign random pointers current = lst.head while current: if current.random: current.next.random = current.random.next current = current.next.next # step 3 # seperate two lists new_list = DoublyList() new_head = lst.head.next new_list.head = new_head old = lst.head new = new_head while old: old.next = new.next old = old.next new.next = old.next if old else None new = new.next if new else None return new_list
def test_prepend_adds_an_element_at_the_beginning(self): first_node = Node(100) node = Node(1000) linked = DoublyLinkedList(first_node) linked.prepend(node) self.assertEqual(linked.as_list(), [1000, 100]) self.assertIs(linked.first_node, node) self.assertIs(linked.first_node.next, first_node) self.assertIs(first_node.previous, node)
def main(): n1 = Node(1, None, None) n2 = Node(2, None, None) n3 = Node(1, None, None) n4 = Node(3, None, None) l = DoubleList() l.append(n1) l.append(n2) l.append(n3) l.append(n4) print(is_palindrome(l))
def enqueue(self, val): """Add new element to end of the queue.""" new_node = Node(self, val) if self.linked_list.head is None: self.linked_list.head = new_node self.linked_list.tail = new_node else: self.linked_list.tail.prev_node == new_node new_node.next_node = self.linked_list.tail self.linked_list.tail == new_node
def insertion_sort(lt, val): if lt.len == 0: lt.addNodeE(Node(val)) else: current = lt.head while True: if current.val <= val: if current.next != None: current = current.next else: lt.addNodeE(Node(val)) break else: node = Node(val) temp = current.prev current.setPrev(node) node.setNext(current) if temp != None: node.setPrev(temp) temp.setNext(node) else: lt.head = node lt.len += 1 break return lt
def test_delete(self): node_list = [ Node(1), Node(1), Node(1), Node(2), Node(2), Node(9), Node(9), Node(10) ] self.list.add_in_tail(node_list[0]) self.list.delete(1) self.assertIsNone(self.list.head) self.assertIsNone(self.list.tail) for node in node_list: self.list.add_in_tail(node) self.list.delete(1) self.assertEqual(self.list.head, node_list[1]) self.assertIsNone(self.list.head.prev) self.list.delete(10) self.assertEqual(self.list.tail, node_list[-2]) self.assertIsNone(self.list.tail.next) self.list.delete(2) self.assertEqual(self.list.head.next.next, node_list[4]) self.assertIsNotNone(self.list.head.next.next.prev) self.assertIsNotNone(self.list.head.next.next.next)
def test_node_has_attributes(): """Test that the node has attributes.""" from doubly_linked_list import Node n = Node(1, None) assert n.data assert n.next_node is None assert n.previous_node is None
def test_insert_at_position_one(self): linked = get_instance() second_node = Node(1000) linked.insert(second_node, 1) self.assertEqual(linked.as_list(), [100, 1000, 200, 300, 400])
def put(self, value): if value in self.cache_table: new_node = self.cache_table[value] if new_node == self.cache_list.tail: return elif new_node == self.cache_list.head: self.cache_list.delete_with_data(new_node) self.cache_list.insert_at_end(new_node) else: self.cache_list.tail.prev.next = new_node new_node.prev.next = self.cache_list.tail #1 new_node.next.prev = self.cache_list.tail #2 self.cache_list.tail.next = new_node.next #3 self.cache_list.tail.prev.next = new_node #4 # import pdb; pdb.set_trace() new_node.prev, self.cache_list.tail.prev = self.cache_list.tail.prev, new_node.prev # 5 and 6 new_node.next = None # 7 self.cache_list.tail = new_node #8 else: new_node = Node(value, None, None) if self.cache_list.length < self.size: self.cache_list.insert_at_end(value) self.cache_table[value] = self.cache_list.tail else: del self.cache_table[self.cache_list.head.data] self.cache_list.delete_from_beginning() self.cache_list.insert_at_end(value) self.cache_table[value] = self.cache_list.tail
def test_node_has_attributes(): """Test node.""" from doubly_linked_list import Node n = Node(1, None, None) assert hasattr(n, 'data') assert hasattr(n, 'next') assert hasattr(n, 'prev')
def test_insert_at_the_end(self): linked = get_instance() second_node = Node(1000) linked.insert(second_node, 3) self.assertEqual(linked.as_list(), [100, 200, 300, 1000, 400])
def test_insert(self): list_test = LinkedList2() node_1000 = Node(1000) list_test.insert(None, node_1000) self.assertEqual(get_list_nodes(list_test), [1000]) self.assertEqual(list_test.tail.next, None) self.assertEqual(list_test.head.prev, None) self.assertEqual(list_test.tail.value, 1000) self.assertEqual(list_test.head.value, 1000) self.assertEqual(list_test.head.next, None) self.assertEqual(list_test.tail.prev, None) list_test = LinkedList2() node_12 = Node(12) node_73 = Node(73) node_1000 = Node(1000) list_test.add_in_tail(node_12) list_test.add_in_tail(node_73) list_test.insert(node_12, node_1000) self.assertEqual(get_list_nodes(list_test), [12, 1000, 73]) self.assertEqual(list_test.head.next.value, 1000) self.assertEqual(list_test.head.next.next.value, 73) self.assertEqual(list_test.tail.prev.value, 1000) self.assertEqual(list_test.tail.next, None) self.assertEqual(list_test.tail.prev.next.value, 73) list_test = LinkedList2() node_12 = Node(12) node_1000 = Node(1000) list_test.add_in_tail(node_12) list_test.insert(node_12, node_1000) self.assertEqual(get_list_nodes(list_test), [12, 1000]) self.assertEqual(list_test.head.next.value, 1000) self.assertEqual(list_test.tail.prev.value, 12) self.assertEqual(list_test.tail.next, None) self.assertEqual(list_test.tail.prev.next.value, 1000) lst = LinkedList2() node_1000 = Node(1000) node_10 = Node(10) node_12 = Node(12) lst.add_in_tail(node_10) lst.add_in_tail(node_12) lst.insert(node_12, node_1000) self.assertEqual(get_list_nodes(lst), [10, 12, 1000]) self.assertEqual(lst.head.next.next.value, 1000) self.assertEqual(lst.tail.prev.value, 12) self.assertEqual(lst.tail.next, None) self.assertEqual(lst.tail.prev.next.value, 1000)
class LRUcache(object): def __init__(self,size=None): self.cache_list = Node() self.cache_table = {} self.size = size def _add_to_list(self,value): new_node = self.cache_table[value] if new_node == self.cache_list.tail: return elif new_node == self.cache_list.head: self.cache_list.delete_with_data(new_node) self.cache_list.insert_at_end(new_node) else: self.cache_list.tail.prev.next = new_node new_node.prev.next = self.cache_list.tail #1 new_node.next.prev = self.cache_list.tail #2 self.cache_list.tail.next = new_node.next #3 self.cache_list.tail.prev.next = new_node #4 new_node.prev, self.cache_list.tail.prev = self.cache_list.tail.prev, new_node.prev # 5 and 6 new_node.next = None # 7 self.cache_list.tail = new_node #8 def put(self,value): # import pdb; pdb.set_trace() if value in self.cache_table: self._add_to_list(value) else: if self.cache_list.length < self.size: self.cache_list.insert_at_end(value) self.cache_table[value] = self.cache_list.tail else: del self.cache_table[self.cache_list.head.data] self.cache_list.delete_from_beginning() self.cache_list.insert_at_end(value) self.cache_table[value] = self.cache_list.tail def get(self,value): if value in self.cache_table: node = self.cache_table[value] self._add_to_list(value) print (node.data) else: return -1
def test_prepend_adds_an_element_at_the_beginning_even_without_a_first_node(self): linked = DoublyLinkedList() node = Node(1000) linked.prepend(node) self.assertEqual(linked.as_list(), [1000]) self.assertIs(linked.first_node, node) self.assertIs(linked.first_node.next, None) self.assertIs(linked.first_node.previous, None)
def test_delete_node(self): """ Tests that a Node can delete itself """ node = Node(1) node.insert_after(2) next = node.next node.insert_before(5) prev = node.prev node.delete() self.assertEqual(next.prev.value, 5) self.assertEqual(prev.next.value, 2)
def __merge_sort(lst, frm, _to): if frm is _to: new_lst = DoublyList() new_node = Node.from_another_node(frm) new_lst.add_node(new_node) return new_lst mid = middle(lst, frm, _to) left = __merge_sort(lst, frm, mid) right = __merge_sort(lst, mid.next, _to) return merge(left, right)
def test_delete_3(self): self.list_test = LinkedList2() self.list_test.delete(12) self.assertEqual(get_list_nodes(self.list_test), []) self.list_test = LinkedList2() self.list_test.add_in_tail(Node(12)) self.list_test.delete(12) self.assertEqual(get_list_nodes(self.list_test), []) self.list_test = LinkedList2() self.list_test.add_in_tail(Node(12)) self.list_test.delete(10) self.assertEqual(get_list_nodes(self.list_test), [12]) self.list_test = LinkedList2() self.list_test.add_in_tail(Node(11)) self.list_test.add_in_tail(Node(12)) self.list_test.add_in_tail(Node(73)) self.list_test.delete(73) self.assertEqual(get_list_nodes(self.list_test), [11, 12])
def setUp(self): self.list_test = LinkedList2() self.list_test.add_in_tail(Node(12)) self.list_test.add_in_tail(Node(73)) self.list_test.add_in_tail(Node(73)) self.list_test.add_in_tail(Node(12)) self.list_test.add_in_tail(Node(73)) self.list_test.add_in_tail(Node(73))
def test_find_all(self): lst = self.list_test.find_all(12) list_value = [node.value for node in lst] self.assertEqual(list_value, [12, 12]) list_test = LinkedList2() self.assertEqual(list_test.find_all(12), []) list_test = LinkedList2() list_test.add_in_tail(Node(12)) lst = list_test.find_all(12) list_value = [node.value for node in lst] self.assertEqual(list_value, [12])
def add(self, elem): self._validate(elem) new = Node(elem) if self.is_empty(): self.head = new self.tail = new else: current = self.head next_ = current.get_next() while current is not None: if self._is_head(new) or self._is_tail(new) or self._is_middle( new, current, next_): return True current = current.get_next() next_ = next_.get_next() return False
def test_find_all(self): node_list = [ Node(1), Node(1), Node(1), Node(2), Node(2), Node(9), Node(9), Node(10) ] for node in node_list: self.list.add_in_tail(node) self.assertEqual(self.list.find_all(1), node_list[0:3]) self.assertEqual(self.list.find_all(2), node_list[3:5]) self.assertEqual(self.list.find_all(9), node_list[5:7]) self.assertEqual(self.list.find_all(10), node_list[-1:]) self.assertEqual(self.list.find_all(3123), [])
def insert(self, key, value): if key in self.cache: node = self.cache[key] node.data = (key, value) self.queue.removeNode(node) self.queue.insert_node(node, 0) else: new_node = Node((key, value)) evicted_value = None if len(self.cache) >= self.capacity: lru_node = self.queue.tail lru_key, evicted_value = lru_node.data del self.cache[lru_key] self.queue.removeNode(lru_node) self.cache[key] = new_node self.queue.insert_node(new_node, 0) return evicted_value
def test_clean(self): node_list = [ Node(1), Node(1), Node(1), Node(2), Node(2), Node(9), Node(9), Node(10) ] for node in node_list: self.list.add_in_tail(node) self.list.clean() self.assertEqual(self.list.len(), 0) self.assertIsNone(self.list.head) self.assertIsNone(self.list.tail)
def test_find(self): node_list = [ Node(1), Node(1), Node(1), Node(2), Node(2), Node(9), Node(9), ] for node in node_list: self.list.add_in_tail(node) self.assertEqual(self.list.find(1), node_list[0]) self.assertEqual(self.list.find(2), node_list[3]) self.assertEqual(self.list.find(9), node_list[5]) self.assertIsNone(self.list.find(123))
def test_delete_all(self): node_list = [ Node(1), Node(1), Node(1), Node(2), Node(2), Node(9), Node(9), Node(10) ] for node in node_list: self.list.add_in_tail(node) self.list.delete(1, True) self.assertEqual(self.list.head, node_list[3]) self.assertIsNone(self.list.head.prev) self.list.delete(10, True) self.assertEqual(self.list.tail, node_list[-2]) self.assertIsNone(self.list.tail.next) self.list.delete(2, True) self.assertEqual(self.list.head, node_list[-3]) self.assertIsNone(self.list.head.prev)
def insert_at_position(DL, n, data): """ This function should insert an element in a doubly linked list at a specific position. E.g. After executing insert_at_position(DL, 2, 88) with DL = 10 <-> 67 <-> 87 <-> 9 It should be DL = 10 <-> 88 <-> 67 <-> 87 <-> 9 Note 1: try to implement this function in the fastest way possible (...how?) Note 2: be careful at how you manage insertion at the head of the list (position 1) and at the tail (position DL.__len__()) :param DL: a doubly linked list :param n: the position at which n should be inserted :param data: the element to insert """ len = DL.__len__() if n > len + 1 or n <= 0: print("Insert fail: wrong position") return if n == len + 1: DL.add(data) return else: new_node = Node(data, None, None) if n < len / 2: walk = DL._head for i in range(n - 1): walk = walk._next if n != 1: new_node._prev = walk._prev walk._prev._next = new_node new_node._next = walk walk._prev = new_node if n == 1: DL._head = new_node else: walk = DL._tail for i in range(len - n): walk = walk._prev new_node._prev = walk._prev walk._prev._next = new_node new_node._next = walk walk._prev = new_node if n == 1: DL._head = new_node return
def addAtStart(self, value): tempNode = Node(value) self.head.prev = tempNode tempNode.next = self.head self.head = tempNode self.printList()