How does Stack work in C #?


I came to a part of my program where I have to apply a stack and wanted someone to give me a simple explanation and an example.

The program that I am doing at the moment is a notepad where you create several "roles" to post something important, with a database to save what was written; but after that my teacher asked for a stack ...

asked by Katomara 11.03.2014 в 23:06

2 answers


I will not repeat what the Miguel Angelo well said.

A stack is called so by in abstract concept. Obviously physically in memory there is no data being piled anywhere.

A stack or stack is a very efficient data structure precisely because it is quite limited. But that limitation fits very well into various problems.

You often need to add elements to a list in a row and remove them from the list in reverse order. This way it is very easy to manipulate the insertion and removal of elements.

This becomes especially favorable if you have a defined stack size that always fits all the elements that need to be placed. But nothing prevents you from having a stack that varies in size. Of course, every time that the space available for the elements needs to be increased or reduced, extra processing is required. But this occurs in blocks (usually doubling every time the stack is full and the reduction is only done by manual request). When there is a defined size an addition "costs" only a pointer change and verification if the stack boundary has not broken.

Afairlytypicalexampleofastackis organization of data in memory in an application. As blocks of code are executed, every necessary data is placed on the stack. And when the execution of the block ends and it is no longer necessary to save that data for other uses, simply move the pointer down (depending on how you are looking the stack can be up).

The solution to a Tower of Hanoi problem also uses a stack.

Compilers and software that do expression analysis also use batteries.

More trivial problems can also be used with batteries. Whenever you have this UEPS (last-in, first-out) feature, the battery should be used. A trivial example is for functionality of undo .


using System;
using System.Collections.Generic;

class Program {
    static Stack<int> MontaPilha() {
        var pilha = new Stack<int>(); //Cria a pilha que vai guardar ints
        pilha.Push(3261); //manda o primeiro elemento para a pilha
        pilha.Push(1352); //vai mais um elemento ficando do seu topo
        pilha.Push(723); //sucessivamente
        return pilha;

    static void Main() {
        var pilha = MontaPilha();
        foreach (int i in pilha) {
            Console.WriteLine(i); //acesa cada inteiro varrendo toda a pilha

        Console.WriteLine(pilha.Pop()); //retira o elemento mais recente colocado na pilha. no exemplo passará ter apenas 3 elementos. Vai imprimir 1234

        Console.WriteLine(pilha.Peek()); //pega o elemento mais recente/topo sem retirá-lo. Vai imprimir 123

        pilha.Clear(); //limpa todos os elementos da pilha

        Console.WriteLine(pilha.Count); //vai imprimir 0

See working on .NET Fiddle . And in the Coding Ground (I post later, technical problems). Also I put it in GitHub for future reference .

07.05.2014 в 16:59

Stack is nothing more than a stack, in which the incoming objects end up blocking those already on the stack, so only the one that has no other locking can be removed from the stack.

Battery Examples:

  • A stack of plates, if you take the bottom, probably everyone will fall

  • A stack of paper, if you try to pull one out of the middle, you can tear

As a data structure, a stack represents this situation, in which several items can be inserted, and can only be removed from the stack in the reverse order of the inserts, ie the last one to be inserted exits first, and the the first one to be entered exits last. This is the name of LIFO (Last-in first-out), which in Portuguese is last-to-enter first-out .

Using in a program

Net already has a stack class: Stack<T> .

It has two important methods, which are equivalent to the actions of adding an object to the top of the Push stack, and another to removing an object from the top of the Pop stack. It also has a very useful property, to indicate how many objects there are in the Count stack.

11.03.2014 в 23:24