Remove specific data on a stack

Asked

Viewed 1,158 times

2

I have no idea how to remove a specific data from a stack. I tried to perform the minhaPilha.remove(); and nothing went right.

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’t do this in a pile. However, I was passed by a master: desempilhe, then remove the desired and so you stack again having removed the desired dice.

  • What do you call "specific data"? Do you want to remove an item that is not the last one? Do you know what a stack is? You know this can’t be done to her?

  • Yes, I know it is not possible. However, I was given this headache by a master and the logic he gave was. Desempilhe, then remove the desired and so you stack again having removed the desired data.

  • When it is so, put all possible information in the question.

  • Thanks, bigown... starting now on the overflow.... Again thank you, vlh

2 answers

1

By definition, 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: the class Stack was deprecated with the introduction of generic types in C# 2.0. You should use generic version: Stack<T>.

That being said, if you really insist on removing a random item from a stack (although it is highly advised), you will need to create a new filtered stack:

stack = new Stack<string>(stack.Where(carta => carta != "Segunda"));

1

I think I should use another structure, maybe even create one that uses a list of 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);
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I haven’t tested it thoroughly, but it should. He does precisely the de-shuffling until he finds what must be removed, and he goes back to stacking what has been shuffled. There are better ways to do this but I did it according to your requirement, which makes no sense, but since it’s exercise, there must be a reason.

Browser other questions tagged

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