Python Forum
About linked lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: About linked lists (/thread-35292.html)



About linked lists - ManoEl - Oct-17-2021

I'm learning Python and would like help to know how I can remove a node from a linked list from a value, currently my code looks like this:

noRaiz = None

def novoNo(valor):
    return {
    "valor": valor,
    "proximo": None
    }

def remove(valor):
    global noRaiz
    if noRaiz is None:
        return
    noAtual = noRaiz
    if noRaiz["valor"] == valor:
        noRaiz = noRaiz["proximo"]
        return
    while noAtual["proximo"] is not None:
        if noAtual["proximo"]["valor"] == valor:
            noAtual["proximo"] = noAtual["proximo"]["proximo"]
        noAtual = noAtual["proximo"]

def imprimir():
    noAtual = noRaiz
    while noAtual is not None:
        print(noAtual["valor"])
        noAtual = noAtual["proximo"]

noRaiz = novoNo(54)
no2 = novoNo(26)
no3 = novoNo(93)
no4 = novoNo(17)
no5 = novoNo(77)
no6 = novoNo(31)

noRaiz["proximo"] = no2
no2["proximo"] = no3
no3["proximo"] = no4
no4["proximo"] = no5
no5["proximo"] = no6
no6["proximo"] = None

imprimir()
remove()
noRaiz is the first node on the list, noAtual is the current node in the list at print time when running the code and imprimir() it's the function to print the nodes. How do I remove the number 54 or 17? I did the remove function but it's probably not right because it doesn't remove.


RE: About linked lists - Gribouillis - Oct-17-2021

remove() cannot work without an argument. You could call remove(17) for example, then imprimir() .


RE: About linked lists - ManoEl - Oct-17-2021

(Oct-17-2021, 02:49 PM)Gribouillis Wrote: remove() cannot work without an argument. You could call remove(17) for example, then imprimir() .

thank you Big Grin