Remove specific data in a stack

2

I have not the slightest idea how to remove a specific data from a stack. I tried to minhaPilha.remove(); and it did not work.

Console.WriteLine("<Pilha>");
Stack<string> minhaPilha = new Stack<string>();
Console.WriteLine("Para add itens, selecione X");
Console.WriteLine();
Console.WriteLine("Para remover itens, selecione Z");
Console.WriteLine();
Console.WriteLine("Caso queira remover um dado especifico digite W");

minhaPilha.Push("Primeira");
minhaPilha.Push("Segunda");
minhaPilha.Push("Terceira");
minhaPilha.Push("Quarta");

String opc = Console.ReadLine();

foreach (string carta in minhaPilha)
{
    Console.WriteLine(carta);
}

if (opc == "Z")
{
    Console.WriteLine();
    minhaPilha.Pop();

    foreach (string carta in minhaPilha)
    {
        Console.WriteLine(carta);
    }

}
if (opc == "X")
{
    Console.WriteLine("Digite aqui:");
    minhaPilha.Push(Console.ReadLine());
    Console.WriteLine();
    foreach (string carta in minhaPilha)
    {
        Console.WriteLine(carta);
    }
}
if (opc == "W")
{
    Console.WriteLine();
    minhaPilha.Pop();

    foreach (string carta in minhaPilha)
    {
        Console.WriteLine(carta);
    }
}
Console.WriteLine();

I know you can not do this on a stack. However, it was passed to me by a master: unstack, then remove the desired one and thus you stack again having taken the desired data.

    
asked by anonymous 03.09.2015 / 13:18

2 answers

1

By default, a stack does not allow random access / insertion / removal.

If you need to remove a specific item, consider using another data structure, such as List<T> .

One more note: Stack class was deprecated with the introduction of generic types in C # 2.0. You should use the generic version: Stack<T> .

That said, if you really insist on removing a random stack item (though it is highly recommended), you will have to create a new, filtered stack:

stack = new Stack<string>(stack.Where(carta => carta != "Segunda"));
    
03.09.2015 / 13:27
1

I think I should use another framework, maybe even create one that uses a list with methods Pop() and Push() . But I'll give you the solution you asked for:

WriteLine("Qual elemento:");
var selecionado = ReadLine();
var novaPilha = new Stack<string>();
var totalItens = minhaPilha.Count;
for (var i = 0; i < totalItens; i++) {
    if (minhaPilha.Peek() == selecionado) {
        minhaPilha.Pop();
        break;
    } else {
        novaPilha.Push(minhaPilha.Pop());
    }
}
totalItens = novaPilha.Count;
for (var i = 0; i < totalItens; i++) {
    minhaPilha.Push(novaPilha.Pop());
}
foreach (var carta in minhaPilha) {
    WriteLine(carta);
}

See running on dotNetFiddle .

I have not tested extensively but should resolve. He just does the uncapping until he finds what should be removed, and back to pile up what has been unstacked. It has better ways of doing this but I did it according to your requirement, which does not make sense, but since it is exercise, it should have a reason.

    
03.09.2015 / 14:15