summaryrefslogtreecommitdiff
path: root/_legacy/linked_list/linked.py
blob: d3f0084105de4a8859d88e363a86bd11445b5c13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import random
def generator(l, up, low, h):
    linked = []
    linkedp = []
    for i in range(l):
        linked.append(random.randint(up, low) if i < h else None)
        if i == h - 1:
            linkedp.append(-1)
            continue
        linkedp.append(i+1)
    return (linked, linkedp, 0, -1, h)
# my_list = [37, 18, 49, 77, 68, None, None, None, None, None]
# my_plist = [-1, 0, 1, 2, 3, 4, 5, 6, 7, -1]
def delete(element, my_list, my_plist, start, null, heap):
    #global start, heap, my_list, null
    if start == null:
        # empty
        print("Linked list empty")
        return None
    index = start
    oldIndex = start
    while my_list[index] != element and index != null:
        oldIndex = index
        index = my_plist[index]
    if index == null:
        print("Item does not exist.")
        return None
    tmp_pointer = my_plist[index]
    my_plist[index] = heap
    my_plist[oldIndex] = tmp_pointer
LENGTH = 15
l,p,s,n,h = generator(LENGTH, 0, 100, round(LENGTH / 2))
for i in range(len(l)):
    print(f"[{i}]\t{l[i]}\t{p[i]}\t{'<- heap' if i == h else ''}{'<- root' if i == s else ''}")
print("-------")
delete(int(input("$ ")), l, p, s, n, h)
for i in range(len(l)):
    print(f"[{i}]\t{l[i]}\t{p[i]}\t{'<- heap' if i == h else ''}{'<- root' if i == s else ''}")