def p_2_5_v1(): # in reversed order num1 = UnList() # use push function to put digit in the first of list. num1.push(6) num1.push(1) num1.push(7) # print('number 1:') print(*num1.traverse(), sep='--->') num2 = UnList() # use push function to put digit in the first of list. num2.push(2) num2.push(9) num2.push(5) res = sum_lists(num1, num2) li = [] while res: li.append(str(res.getData())) res = res.getNext() print(*li, sep='--->') # print ( 'number 2:' ) print(*num2.traverse(), sep='--->') # now we have two nubers in reversed order so firs of all get each number # digits in one integer number if we just leav as it traversed will be # in wrong order like for 617 will be 716 so to reverse it use reversed # function implmented in linked list class # num1.reverse() # num2.reverse() # number1 = int( "".join(map(str, num1.traverse())) ) # number2 = int( "".join(map(str, num2.traverse())) ) # # print("=====================================") # # print(number1, number2) # # s_n_1_2 = number1 + number2 # print("number1 + number 2 = " + str(s_n_1_2)) # # # now store it in linked list reversed to be 912 ==> 219 # result = UnList() # for digit in str(s_n_1_2): # result.push(digit) # # print("Result:") # print(*result.traverse(), sep="--->") return
def p_2_7(): myList = UnList() myList.append(1) myList.append(2) myList.append(3) myList.append(4) myList.append(3) myList.append(2) myList.append(1) # myListRevesed = myList # # myListRevesed.reverse() # # l1 = myList.traverse() # l2 = myListRevesed.traverse() # # error = False # for i in range( (len(l1) // 2) + 1 ): # if l1[i] != l2[i]: # error = True # break # # print(not error) print(myList.isPalindrome())
def p_2_6(): circ = UnList() circ.append('A') circ.append('B') circ.append('C') circ.append('D') circ.append('E') circ.append('C') print(circ.findStartOfLoop()) return
def p_2_5_v2(): # in normal order num1 = UnList() # use push function to put digit in the first of list. num1.append(6) num1.append(1) num1.append(7) # print('number 1:') print(*num1.traverse(), sep='--->') num2 = UnList() # use push function to put digit in the first of list. num2.append(2) num2.append(9) num2.append(5) # print ( 'number 2:' ) print(*num2.traverse(), sep='--->') # now we have two nubers in reversed order so firs of all get each number # digits in one integer number number1 = int("".join(map(str, num1.traverse()))) number2 = int("".join(map(str, num2.traverse()))) # print("=====================================") # # print(number1, number2) # s_n_1_2 = number1 + number2 # print("number1 + number 2 = " + str(s_n_1_2)) # # now store it in linked list result = UnList() for digit in str(s_n_1_2): result.append(digit) print("Result:") print(*result.traverse(), sep="--->")
def p_2_4(): myList = UnList() myList.append(4) myList.append(5) myList.append(2) myList.append(1) myList.append(3) myList.append(6) X = 3 print('Before:') print(*myList.traverse(), sep='--->') myList.partition(X) # myList.sort() print("After:") print(*myList.traverse(), sep='--->')
def p_2_1(): """ Write code to remove duplicates from an unsorted linked list. :return: void """ myList = UnList() myList.append(1) myList.append(3) myList.append(2) myList.append(1) myList.append(4) myList.append(2) myList.append(3) print('Before:') print(*myList.traverse(), sep='--->') myList.removeDuplicates() print("After:") print(*myList.traverse(), sep='--->')
def p_2_2(): myList = UnList() myList.append(1) myList.append(3) myList.append(2) myList.append(1) myList.append(4) myList.append(2) myList.append(3) k = 8 # get the size of list list_len = myList.size() nodes = [] for i in range(k, list_len + 1): el = myList.findByPos(i) if el < 0: break nodes.append(str(el)) print(*nodes, sep='--->') if nodes else print('No elemnts')
def p_2_3(): myList = UnList() myList.append(1) myList.append(2) myList.append(3) myList.append(4) myList.append(5) myList.append(6) myList.append(7) # delete by position deleted_node_pos = 3 # delete by node value deleted_node = 8 print('Before:') print(*myList.traverse(), sep='--->') # myList.popByPos ( deleted_node_pos ) myList.remove(deleted_node) print("After:") print(*myList.traverse(), sep='--->')