How to delete node in linked list?

-3

How to delete the middle value from a linked list C #

Element Class;

public class NoLista
{
    public float info;
    public NoLista proximo;

    //construtor para passar valor ao elemento
    public NoLista(int valor)
    {
        this.info = valor;
        this.proximo = null;
    }
}

Class for operations

    public class Lista
{
    private NoLista inicio;
    public Lista()
    {
        this.inicio = null;
    }

    public void Remover(float valor)
    {
        if (this.inicio == null)
        {
            Console.WriteLine("o Valor preenchido nao existe na Lista");
        }
        else
        {
            NoLista antecesser = this.inicio;

            if(antecesser.info != valor)
            {
                 antecesser = this.inicio.proximo;
            }

            //aqui quero deletar o valor


        }
    }
    
asked by anonymous 11.11.2015 / 19:30

3 answers

0

Your question is not very clear.

What I understood was that you have a list of 4 elements and want to remove a specific one. This can be done as follows:

var lista = new List<int> {1, 2, 3, 4}; //Criar um list com 4 elementos
lista.Remove(3); //Remover o item com o valor "3"
    
11.11.2015 / 20:07
0

The algorithm looks something like this:

"Be NoAtual the first node in the list.

While NoAtual is not null:

If the value saved by NoAtual is different from the value that I want to remove, NoAtual receives the successor of NoAtual . Then, continue .

Otherwise (that is, if the saved value is the same as the one I want to remove):

If NoAtual is the first in the list, make the inicio of the list point to the successor of NoAtual . Destroy NoAtual . Please shut down.

Otherwise (if it is not the first one in the list), then the successor of the NoAtual predecessor becomes the successor of NoAtual . Destroy NoAtual . Close. "

In C #, you do not need to explicitly destroy the elements you want to delete, because the execution environment takes care of this for you.

It is an exercise for you to find out which variables you will have to save in order to link the predecessor's references to the successor.

    
12.11.2015 / 03:17
0

Once you find No with the desired value, you only need to update the property next to the previous No or the start property of the list.

You do not have to delete the No, just destroy whatever reference to it, then let the GC decide when and how it ends.

public void Remover(float valor)
{
    if (this.inicio == null)
    {
        Console.WriteLine("A lista está vazia");
        return;
    }

    //buscando o nó com o valor informado.
    var noAnterior = default(NoLista);
    var noAtual = this.inicio;
    do
    {
        if (noAtual.info == valor)
        {
            break;
        }
        noAnterior = noAtual;
        noAtual = noAtual.proximo;
    } while(noAtual != null);

    if (noAtual == null)
    {
        Console.WriteLine("O valor não foi encontrado");
        return;
    }

    //nó a ser apagado é o no inicial, então setar o proximo o como o inicial.
    if (noAnterior == null)
    {
        this.inicio = noAtual.proximo;
    }
    else
    {
        noAnterior.proximo = noAtual.proximo;
    }
}

You can see the example above working by accessing the following .NET Fiddle

    
12.11.2015 / 13:55