S
S
sabn1k2016-02-18 20:09:15
C++ / C#
sabn1k, 2016-02-18 20:09:15

How does the stack work?

I did not quite understand how the stack works in this code:

class Stack
{
  static const int MAX = 10;
  int st[MAX];
  int top;
public:
  Stack():top(0){}
  void push(int var)
  {
    st[++top] = var;
  }
  int pop()
  {
    return st[top--];
  }
};
int main()
{
  setlocale(0, "");
  Stack s1;
  s1.push(11);
  s1.push(22);
  cout << "1: " << s1.pop() << endl;
  cout << "2: " << s1.pop() << endl;
  s1.push(33);
  s1.push(44);
  s1.push(55);
  s1.push(66);
  cout << "3: " << s1.pop() << endl;
  cout << "4: " << s1.pop() << endl;
  cout << "5: " << s1.pop() << endl;
  cout << "6: " << s1.pop() << endl;
  getch();
  return 0;
}

Why is the conclusion like this?
1: 22
2: 11
3: 66
4: 55
5: 44
6: 33

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RedHairOnMyHead, 2016-02-18
@sabn1k

Last in, first out.
If you stack 1 sheet and then pick up 1 sheet at a time, you will take the one that is on top (i.e. the one that was put last).

I
iv_k, 2016-02-19
@iv_k

the stack works like a magazine for a Kalashnikov assault rifle.
and AK itself is a stack-to-queue converter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question