How to delete node in linked list?

Asked

Viewed 167 times

-3

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

Class of Elements;

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


        }
    }
  • 3

    Show what you’ve done, and indicate what you’re having trouble with.

  • var numeros = new LinkedList<int>(new int[] { 1, 2, 3, 4 });&#xA;numeros.Remove(3);

  • ready, implemented the question.

3 answers

0

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

You don’t need to delete No, just destroy which reference you want it to, so let GC decide when and how to end it.

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 in operation by accessing the following .NET Fiddle

0

Your question is not very clear.

What I understood was that you have a list with 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"

0

The algorithm goes something like this:

"Be it NoAtual the first node in the list.

While NoAtual is not null:

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

Otherwise (ie if the saved value is equal to the one I want to remove):

If NoAtual is the first on the list, do the inicio from the list point to the successor of NoAtual. Destroy NoAtual. Wax.

Otherwise (if not the first on the list), then the successor to the predecessor of NoAtual becomes the successor of NoAtual. Destroy NoAtual. Wax."

In C#, it is not necessary to explicitly destroy the elements you want to delete, as the execution environment takes care of it for you.

It’s a workout for you to figure out which variables you’ll have to keep in order to connect the predecessor’s references to the successor.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.