Answer the question
In order to leave comments, you need to log in
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;
}
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
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).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question