def push(self): if not self.top: self.top = Node(data) else: n = Node(data) n.nxt = self.top self.top = n
def create_linked_list(n): head = Node(1) p = head for i in range(n-1): p.nxt = Node( random.choice(range(5,9)) ) p = p.nxt return head
def create_unique_linked_list(n): used_items = range(n) item = used_items.pop( random.choice(range(n)) ) head = Node(item) p = head for i in range(n-1): item = used_items.pop( random.randrange(len(used_items)) ) p.nxt = Node(item) p = p.nxt return head
def add_two_lists(l1, l2): if not l1: return l2 if not l2: return l1 p1 = l1 p2 = l2 result = None p = None carry = 0 while p1 and p2: s, carry = add_num(p1.data, p2.data, carry) if not result: result = Node(s) p = result else: p.nxt = Node(s) p = p.nxt p1 = p1.nxt p2 = p2.nxt t = p1 if p1 else p2 while t: s, carry = add_num(t.data, 0, carry) p.nxt = Node(s) p = p.nxt t = t.nxt if carry: p.nxt = Node(1) return result
def make_list(num): if num is None: return n = num result = None p = None while n > 0: node = Node(n % 10) if not result: result = node p = result else: p.nxt = node p = p.nxt n = n / 10 return result
def test_setting_next(self): node = Node([]) node.set_next(Node(3)) self.assertEqual(node.get_next().get_data(), 3)
def enqueue(self, d): if not self.end: self.front = self.end = Node(d) else: self.end.nxt = Node(d) self.end = self.end.nxt
def test_data_and_next(self): node_list = [Node(item) for item in self.input_list] for node, inp in zip(node_list, self.input_list): self.assertEqual(node.get_data(), inp) self.assertIsNone(node.get_next())
def test_object_creation(self): node_list = [Node(item) for item in self.input_list] for node in node_list: self.assertIsNotNone(node)