summaryrefslogtreecommitdiff
path: root/linked_list/linked.py
blob: cd5d36e890ce0aa72ca5582820b9b6f9f33cddd0 (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
39
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 == 0:
            linkedp.append(-1)
            continue
        linkedp.append(i-1)
    return (linked, linkedp, h-1, -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
    heap = index
    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 ''}")
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 ''}")