def add_1(l1, l2): n1 = l1.get_head() n2 = l2.get_head() # to manage new list where sum is stored head = ll.Node(-1) tail = head val = 0 # while stops if both lists reach to the end while n1 != None or n2 != None: if n1 == None: val = val + n2.data n2 = n2.next elif n2 == None: val = val + n1.data n1 = n1.next else: val = val + n1.data + n2.data n1 = n1.next n2 = n2.next temp = ll.Node(val % 10) tail.next = temp tail = temp val = int(val / 10) return head.next
def create_testcases(val): # creating fist section of list x = random.randint(1, 8) count = 0 while count < x: temp = ll.Node(random.randint(1, 20)) if count == 0: head1 = temp else: tail1.next = temp tail1 = temp count += 1 # creating second section of list x = random.randint(1, 10) count = 0 while count < x: temp = ll.Node(random.randint(25, 40)) if count == 0: head2 = temp else: tail2.next = temp tail2 = temp count += 1 # if val == '1', we create a linkedlist with a cycle else without cycle if val == 1: tail1.next = head2 tail2.next = tail1 else: tail1.next = head2 return (head1, tail1)
def enqueue(self, data): if self.first == None: self.first = linkedlist.Node(data=data) self.last = self.first self.last.next = linkedlist.Node(data=data) self.last = self.last.next
def create_testcases(val): # creating fist section of l1 x = random.randint(1, 7) count = 0 while count < x: temp = ll.Node(random.randint(20, 30)) if count == 0: head1 = temp else: tail1.next = temp tail1 = temp count += 1 # creating fisrt section of l2 x = random.randint(1, 8) count = 0 while count < x: temp = ll.Node(random.randint(30, 40)) if count == 0: head2 = temp else: tail2.next = temp tail2 = temp count += 1 # creating second section of lists head3 = None x = random.randint(1, 6) print("the value of x is :", x) count = 0 while count < x: temp = ll.Node(random.randint(1, 20)) temp.next = head3 head3 = temp count += 1 head4 = None x = random.randint(1, 7) count = 0 while count < x: temp = ll.Node(random.randint(1, 20)) temp.next = head4 head4 = temp count += 1 # for val == 1 create positive testcase and for '0' create negative testcase if val == 1: tail1.next = head3 tail2.next = head3 else: tail1.next = head3 tail2.next = head4 print("list that is common to both : ") print_list(head3) return (head1, head2)
def add_2_stack(l1, l2): n1 = l1.get_head() n2 = l2.get_head() # to store the input lists stack1 = [] stack2 = [] # for calculating the lengths of input linkedlist length1 = 0 length2 = 0 # pushing the digits of each list onto the stack while n1 != None or n2 != None: if n1 == None: stack2.append(n2.data) n2 = n2.next length2 += 1 elif n2 == None: stack1.append(n1.data) n1 = n1.next length1 += 1 else: stack1.append(n1.data) stack2.append(n2.data) length1 += 1 length2 += 1 n1 = n1.next n2 = n2.next # finding the shortest list if length1 > length2: length = length1 else: length = length2 # to manage new list where sum is stored head = ll.Node(-1) val = 0 # poping from stack, performing addition, creating new list while length: x = 0 y = 0 if stack1: x = stack1.pop() if stack2: y = stack2.pop() val = val + x + y temp = ll.Node(val % 10) val = int(val / 10) temp.next = head.next head.next = temp length -= 1 return head.next
def mejorCamino(self): menorValor = 0 ultimaPosicion = -555 # En caso de que se necesite ir a la ultima posición, se la guarda. seEncontroComparacion = False seEncontroPosicion = False for i in range(0, len(self.agent.decisiones)): if (self.agent.decisiones[i] != -1): if (self.agent.ultimaPosicion != i): #print("Empezando por ",i) menorValor = i seEncontroComparacion = True break else: ultimaPosicion = i if (seEncontroComparacion == False): menorValor = ultimaPosicion for i in range(menorValor + 1, len(self.agent.decisiones)): if (self.agent.decisiones[i] < self.agent.decisiones[menorValor] and self.agent.decisiones[i] != -1): if (self.agent.ultimaPosicion != i): menorValor = i seEncontroPosicion = True else: #print("Guardando ultimaposicion: ", i) ultimaPosicion = i if (seEncontroPosicion == False): if (seEncontroComparacion == False): #print("Se necesita la ultima posicion: ", i, ", reemplazando: ", menorValor) menorValor = ultimaPosicion self.guardarUltimaUbicacion(menorValor) if (menorValor == 0): #print("arriba", " menorvalor: " , menorValor) self.agent.posicionX = self.agent.posicionX - 1 elif (menorValor == 1): # print("abajo", " menorvalor: " , menorValor) self.agent.posicionX = self.agent.posicionX + 1 elif (menorValor == 2): #print("izq", " menorvalor: " , menorValor) self.agent.posicionY = self.agent.posicionY - 1 else: # print("der" , " menorvalor: " , menorValor) self.agent.posicionY = self.agent.posicionY + 1 # Las siguientes LinkedList son utilizadas para guardar las posiciones donde debe mostrarse una flecha por donde pasa el Agente. L = linkedlist.LinkedList() newNode = linkedlist.Node() newNode.value = self.agent.posicionX L.head = newNode newNode2 = linkedlist.Node() newNode2.value = self.agent.posicionY L.head.nextNode = newNode2 newNode3 = linkedlist.Node() newNode3.value = menorValor L.head.nextNode.nextNode = newNode3 linkedlist.add(self.caminoHecho, L)
def padzerosto_list(head, num): while num: node = ll.Node(0) node.next = head head = node num -= 1 return head
def add_same_size(a, b, carry): if not a: return None result = linkedlist.Node() result.next, carry = add_same_size(a.next, b.next, carry) sum = a.data + b.data + carry carry = sum / 10 result.data = sum % 10 return result, carry
def add_recursion(n1, n2, head): if n1 == None and n2 == None: return 0 else: val = n1.data + n2.data + add_recursion(n1.next, n2.next, head) temp = ll.Node(val % 10) val = int(val / 10) temp.next = head.next head.next = temp return val
def Partition2(l, x): last_node_left = l.head current = l.head previous = l.head while current: if current.value < x: previous.next = current.next node = linkedlist.Node(current.value) node.next = l.head l.head = node previous = current current = current.next
def partition(input_list, x): # pointers to add at the end of two lists n1 = ll.Node(-1) n2 = ll.Node(-1) # head pointers of both the lists l1_head = n1 l2_head = n2 node = input_list.get_head() while node: if node.data < x: n1.next = node n1 = node else: n2.next = node n2 = node node = node.next n2.next = None n1.next = l2_head.next return l1_head.next
def add_2_recursion(l1, l2): n1 = l1.get_head() n2 = l2.get_head() len1 = get_listlength(n1) len2 = get_listlength(n2) if len1 < len2: n1 = padzerosto_list(n1, len2 - len1) else: n2 = padzerosto_list(n2, len1 - len2) head = ll.Node(-1) add_recursion(n1, n2, head) return head.next
def AddListsRecursively(n1, n2, modulo): if n1 == None and n2 == None and modulo == 0: return None base = 10 value = modulo if n1: value += n1.value if n2: value += n2.value result = linkedlist.Node(value % base) if n1 != None or n2 != None: digit = AddListsRecursively(None if n1 == None else n1.next, None if n2 == None else n2.next, value / base) result.setNextNode(digit) return result
def test_Add(self): testList = ll.SinglyLinkedList() testList.add(ll.Node(5)) self.assertEqual(testList.head.data, 5)
import linkedlist def GetIntersectingNode(l1, l2): curr1 = l1.head curr2 = l2.head while curr1: while curr2: if curr1 == curr2: return curr1 curr2 = curr2.next curr1 = curr1.next n1 = linkedlist.Node(3) n2 = linkedlist.Node(7) l1 = linkedlist.CreateList([1, 2, 3]) l1.addNode(n1) l2 = linkedlist.LinkedList() l2.addNode(linkedlist.Node(1)) l2.addNode(n1) l2.addNode(linkedlist.Node(7)) l2.addNode(linkedlist.Node(8)) print n1 print GetIntersectingNode(l1, l2)
def __init__(self, data=None): self.top = linkedlist.Node(data=data)
def test_AddAtIndex2(self): testList = ll.SinglyLinkedList() testList.add(ll.Node(1)) testList.add(ll.Node(3)) testList.addAtIndex(ll.Node(2), 1) self.assertEqual(testList.head.next.data, 2)
def __init__(self, data=None): if data == None: self.first = None self.last = None self.first = linkedlist.Node(data=data) self.last = self.first
def push(self, data): top = linkedlist.Node(data=data) top.next = self.top self.top = top
import linkedlist as ll if __name__ == "__main__": e1 = ll.Node(1) e2 = ll.Node(2) e3 = ll.Node(3) e4 = ll.Node(4) e5 = ll.Node(5) e6 = ll.Node(6) llist = ll.Linkedlist() llist.append(e1) llist.append(e2) llist.append(e3) llist.append(e4) llist.append(e5) llist.append(e6) llist.printlist() llist.pop() llist.insert(3, e6) llist.printlist() print(llist.get(llist.size())) print(llist.get_first()) print(llist.get_last()) # llist.delete(llist.size() # llist.append(e3) # print(llist.to_array()) llist2 = ll.linkedlist() print(llist2.get_first()) print(llist2.get_last())
def push(result, sum): node = linkedlist.Node(sum) node.next = result return node
import linkedlist l = linkedlist.LinkedList() n = linkedlist.Node("c") l.add("a") l.add("b") l.addNode(n) l.add("d") l.add("e") l.add("f") def RemoveNode(node): if node == None or node.next == None: return False node.value = node.next.value node.next = node.next.next return True print l RemoveNode(n) print l