def test_unshift(): colors = SingleLinkedList() colors.push("Viridian") colors.push("Sap Green") colors.push("Van Dyke") assert colors.unshift() == "Viridian" assert colors.unshift() == "Sap Green" assert colors.unshift() == "Van Dyke" assert colors.unshift() == None
def test_pop(): colors = SingleLinkedList() colors.push("Magenta") colors.push("Alizarin") assert colors.pop() == "Alizarin" assert colors.pop() == "Magenta" assert colors.pop() == None
class Queue: def __init__(self): # initialize a linked list to store item self.linked_list = SingleLinkedList() return def enqueue(self, item): # put the item to the tail of queue self.linked_list.add_list_item(item) return def dequeue(self): # to return the first item of the queue and remove it if self.linked_list.head is not None: current_node = self.linked_list.head self.linked_list.head = self.linked_list.head.next return current_node.data def empty(self): # to clear all the items in the queue self.linked_list.head = None return def size(self): # to return the length of the queue size = 0 current_node = self.linked_list.head while current_node is not None: size = size + 1 current_node = current_node.next return size def output_list(self): # outputs the queue (the value of the node, actually) self.linked_list.output_list() return
def get_loop_linked_list(self, n): sll = SingleLinkedList() head = sll.add_item_from_arr(list(range(n))) if head is None: return None, None ptr = head while ptr.next: ptr = ptr.next ptr.next = head return head, ptr ## 直接用list来模拟环形链表,找到删除数字之间的下标变化规律 def LastRemaining_Solution(self, n, m): if n < 1 or m < 1: return -1 arr = list(range(n)) index = (m - 1) % len(arr) while len(arr) > 1: arr.pop(index) index = (index + m - 1) % len(arr) return arr[0]
def test_free_tests(self, n_nodes): print("\n ** FREE TESTS **") """Actions: add, search and remove N (define below) items Expected result: Find all items and have empty list "()" """ N = n_nodes obj = SingleLinkedList() obj_dataset = set() while len(obj_dataset) < N: obj_dataset.add(randint(0,N)) # this approach is not good, but works for simple case. obj_data = [e for e in obj_dataset] print("TEST DATA : Input {}".format(obj_data)) shuffle(obj_data) for d in obj_data: obj.insert(d) #print("After inserting {}, we got {}".format(d, obj)) print("Resulted {}: {}".format(type(obj), obj)) shuffle(obj_data) for d in obj_data: temp_obj = obj.search(d) if temp_obj is None: print("FAILED to find existing item {}".format(d)) obj_data = sorted(obj_data) for i in range(0, len(obj_data)-1): temp_obj = obj.successor(obj_data[i]) if temp_obj != obj_data[i+1]: print("FAILED to find sucessor item {}".format(d)) obj_data = sorted(obj_data, reverse=True) for i in range(0, len(obj_data)-1): temp_obj = obj.predecessor(obj_data[i]) if temp_obj != obj_data[i+1]: print("FAILED to find predecessor item {}".format(d)) shuffle(obj_data) for d in obj_data: result = obj.delete(d) #print("After deleting {}, we got {}".format(d, obj)) if result is None: print("FAILED : could not delete {}.".format(d)) if str(obj) != "()": print("FAILED : obj is not empty after deleting all nodes. Got {}".format(obj)) elif len(obj) != 0: print("FAILED : obj is not empty after deleting all nodes. Tree is has {} nodes".format(len(obj))) else: print("INSERTED, SEARCHED, SUCCESSOR, PREDECESSOR, DELETED {} items.".format(N))
def test_push(): colors = SingleLinkedList() colors.push("Pthalo Blue") assert colors.count() == 1 colors.push("Ultramarine Blue") assert colors.count() == 2
# 迭代解法 def ReverseList(self, pHead): # write code here p_reverse_head = None cur_node = pHead pre_node = None while cur_node is not None: next_node = cur_node.next ## 需要提前保存好下一个节点的值 if next_node is None: p_reverse_head = cur_node cur_node.next = pre_node pre_node = cur_node cur_node = next_node return p_reverse_head ## 递归解法 def ReverseList(self, pHead): if not pHead or not pHead.next: return pHead else: pReversedHead = self.ReverseList(pHead.next) pHead.next.next = pHead ## 把None换掉 pHead.next = None ## 最后要接一个None return pReversedHead sll = SingleLinkedList() sll.add_item_from_arr([1, 2, 3, 4]) sol = Solution() res = sol.ReverseList(sll.head) pass
def test_add(self): print 'whee' my_list = SingleLinkedList() my_list.add(1) self.assertEqual(my_list.head.data, 1)
def _createHashTable(self): for i in range(self.size): self.buckets.append(SingleLinkedList())
def test_last(): colors = SingleLinkedList() colors.push("Cadmium Red Light") assert colors.last() == "Cadmium Red Light" colors.push("Hansa Yellow") assert colors.last() == "Hansa Yellow" colors.shift("Pthalo Green") assert colors.last() == "Hansa Yellow"
pointer.next = pHead2 pHead2 = pHead2.next pointer = pointer.next if pHead1: pointer.next = pHead1 if pHead2: pointer.next = pHead2 return dummy.next ## 递归代码 def Merge(self, pHead1, pHead2): if not pHead1: return pHead2 if not pHead2: return pHead1 if pHead1.val < pHead2.val: pHead1.next = self.Merge(pHead1.next, pHead2) return pHead1 else: pHead2.next = self.Merge(pHead1, pHead2.next) return pHead2 sll = SingleLinkedList() pHead1 = sll.add_item_from_arr([2, 3, 6]) pHead2 = sll.add_item_from_arr([9999]) sol = Solution() res = sol.Merge(pHead1, pHead2) pass
def _create_obj(self,all_nodes): sl = SingleLinkedList() for node in all_nodes: sl.insert(node) return sl
def setUp(self): self.node1 = Node(1) self.node2 = Node(2) self.node3 = Node(3) self.linked_list = SingleLinkedList()
class TestSingleLinkedListWithoutTail(unittest.TestCase): def setUp(self): self.node1 = Node(1) self.node2 = Node(2) self.node3 = Node(3) self.linked_list = SingleLinkedList() def test_head_is_none_by_default(self): self.assertIsNone(self.linked_list.head) def test_prepend_to_empty_list(self): node = Node(1) self.linked_list.prepend(node) self.assertEqual(self.linked_list.head.data, node.data) self.assertIsNone(self.linked_list.head.next) def test_prepend_to_non_empty_list(self): node1 = Node(1) node2 = Node(2) self.linked_list.prepend(node1) self.linked_list.prepend(node2) self.assertEqual(self.linked_list.head.data, node2.data) self.assertEqual(self.linked_list.head.next, node1) self.assertEqual(self.linked_list.head.next.data, node1.data) self.assertIsNone(self.linked_list.head.next.next) def test_pop_front_empty_linked_list(self): self.assertIsNone(self.linked_list.pop_front()) def test_top_front(self): self.assertIsNone(self.linked_list.top_front()) self.linked_list.prepend(self.node1) self.assertTrue(self.linked_list.top_front(), self.node1) def test_pop_front_non_empty_linked_list(self): node1 = Node(1) node2 = Node(2) node3 = Node(3) self.linked_list.prepend(node1) self.linked_list.prepend(node2) self.linked_list.prepend(node3) self.assertTrue(self.linked_list.pop_front(), node3) self.assertTrue(self.linked_list.pop_front().data, node2.data) def test_top_back_empty_linked_list(self): self.assertIsNone(self.linked_list.top_back()) def test_top_back_non_empty_linked_list(self): self.linked_list.prepend(self.node1) self.linked_list.prepend(self.node2) self.assertEqual(self.linked_list.top_back(), self.node1) def test_pop_back_empty_linked_list(self): self.assertIsNone(self.linked_list.pop_back()) def test_pop_back_single_value_linked_list(self): self.linked_list.prepend(self.node1) self.assertEqual(self.linked_list.pop_back(), self.node1) def test_pop_back_multi_value_linked_list(self): self.linked_list.prepend(self.node1) self.linked_list.prepend(self.node2) node3 = Node(3) self.linked_list.prepend(node3) self.assertEqual(self.linked_list.pop_back(), self.node1) self.assertEqual(self.linked_list.pop_back(), self.node2) def test_find_key_in_empty_linked_list(self): self.assertFalse(self.linked_list.find(1)) def test_find_key_in_non_empty_list(self): self.linked_list.prepend(self.node1) self.linked_list.prepend(self.node2) self.assertTrue(self.linked_list.find(1)) def test_remove_blank_linked_list(self): self.assertIsNone(self.linked_list.remove(1)) def test_remove_key_from_single_item_linked_list(self): # TODO check if linked list object changed # comp_linked_list = SingleLinkedList() self.linked_list.prepend(self.node1) self.assertEqual(self.linked_list.remove(1), 1) # self.assertIs(comp_linked_list, self.linked_list) def test_remove_key_from_two_items_linked_list(self): self.linked_list.prepend(self.node1) self.linked_list.prepend(self.node2) self.assertEqual(self.linked_list.remove(2), 2) def test_remove_key_from_multi_items_linked_list(self): self.linked_list.prepend(self.node1) self.linked_list.prepend(self.node2) self.linked_list.prepend(Node(3)) self.assertEqual(self.linked_list.remove(2), 2) def test_is_empty(self): self.assertTrue(self.linked_list.is_empty()) self.linked_list.prepend(self.node2) self.assertFalse(self.linked_list.is_empty()) def test_insert_before_if_prev_node(self): self.linked_list.prepend(self.node1) self.linked_list.prepend(self.node2) self.linked_list.prepend(self.node3) self.linked_list.add_before(3, 4) self.assertTrue(self.linked_list.find(4)) self.assertEqual(self.linked_list.add_before(3, 4), 4) def test_insert_before_if_prev_none(self): self.assertIsNone(self.linked_list.add_before(1, 1)) self.linked_list.prepend(self.node1) self.linked_list.add_before(1, 1)
def __init__(self): # initialize a linked list to store item self.linked_list = SingleLinkedList() return
def test_shift(): colors = SingleLinkedList() colors.shift("Cadmium Orange") assert colors.count() == 1 colors.shift("Carbazole Violet") assert colors.count() == 2 assert colors.pop() == "Cadmium Orange" assert colors.count() == 1 assert colors.pop() == "Carbazole Violet" assert colors.count() == 0
def test_remove(): colors = SingleLinkedList() colors.push("Cobalt") colors.push("Zinc White") colors.push("Nickle Yellow") colors.push("Perinone") assert colors.remove("Cobalt") == 0 assert colors.remove("Perinone") == 2 assert colors.remove("Nickle Yellow") == 1 assert colors.remove("Zinc White") == 0
def test_remove(): colors = SingleLinkedList() colors.push("Cobalt") colors.push("Zinc White") colors.push("Nickle Yellow") colors.push("Perinone") assert colors.remove("Cobalt") == 0 colors.dump("before perinone") assert colors.remove("Perinone") == 2 colors.dump("after perinone") assert colors.remove("Nickle Yellow") == 1 assert colors.remove("Zinc White") == 0
def test_get(): colors = SingleLinkedList() colors.push("Vermillion") assert colors.get(0) == "Vermillion" colors.push("Sap Green") assert colors.get(0) == "Vermillion" assert colors.get(1) == "Sap Green" colors.push("Cadmium Yellow Light") assert colors.get(0) == "Vermillion" assert colors.get(1) == "Sap Green" assert colors.get(2) == "Cadmium Yellow Light" assert colors.pop() == "Cadmium Yellow Light" assert colors.get(0) == "Vermillion" assert colors.get(1) == "Sap Green" assert colors.get(2) == None colors.pop() assert colors.get(0) == "Vermillion" colors.pop() assert colors.get(0) == None
def test_single_linked_list(): # FillConstant @pytest.fixture def element_A(): return { "event_tracking_name": "VISIT", "event_trigger": "HOMEPAGE", "client_timestamp": 12879818279387, } node1 = ListNode(1) node2 = ListNode(2) node3 = ListNode(3) node4 = ListNode(4) node5 = ListNode(5) node6 = ListNode(6) node7 = ListNode(7) node8 = ListNode(8) node9 = ListNode(9) nodeA = ListNode("A") nodeB = ListNode("B") nodeC = ListNode("C") link1 = SingleLinkedList() link1.add_list_item(node1) link1.add_list_item(node2) link1.add_list_item(node3) link1.add_list_item(node4) link1.add_list_item(node5) link1.add_list_item(node6) link1.add_list_item(node7) link1.add_list_item(node8) link1.add_list_item(node9) assert link1.list_length() == 9 print("remove value 5") link1.remove_by_value(5) assert link1.list_length() == 8 assert link1.head.data == 1 assert link1.tail.data == 9 link1.remove_by_value(9) assert link1.list_length() == 7 assert link1.head.data == 1 assert link1.tail.data == 8 res = link1.search(3) assert res == [3]
return count ## 解法三:调用辅助栈(利用栈后入先出的特性) if not pHead1 or not pHead2: return None stack1, stack2 = [], [] while pHead1: stack1.append(pHead1) pHead1 = pHead1.next while pHead2: stack2.append(pHead2) pHead2 = pHead2.next res_temp1 = None while stack1 and stack2: temp1 = stack1.pop() temp2 = stack2.pop() if temp1.val != temp2.val: return res_temp1 res_temp1 = temp1 return res_temp1 sll = SingleLinkedList() pHead1 = sll.add_item_from_arr([1, 2, 3, 4, 5]) pHead2 = sll.add_item_from_arr([1, 2, 3, 4, 5]) sol = Solution() res = sol.FindFirstCommonNode(pHead1, pHead2) pass
def test_many(self): my_list = SingleLinkedList() for data in range(5): my_list.add(data) self.assertEqual(list(my_list), range(5))