Help me with stack

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

class stack{
private:
	const int s=10;
	int a[s];
	int top;
public:
	void getsize(int x);
	void push(int x);
	void pop(int &x);
}


I need those three function definitions. Can anyone help?
Why don't you have a go first?

What do you think the definitions should look like?
My advice is that you try something and post, then we help. Otherwise many people will ignore you.

Aceix.
I understand how I push and pop. But I dont have any idea about getsize() and what should I write in that function. plz help.
Yaa I can try if I've the getsize(). Otherwise how??
what is getsize() supposed to do?
what is the parameter int x good for in getsize()
getsize(): Maximum size of stack; I think so..
getsize() as the name is,sounds like a getter member function. I think it must return the size of the current stack...unless this is a special case?

Aceix
I think it must return the size of the current stack...

Its true. But how can I get? Yet I've initialized the array size as 10.
Last edited on
I believe this should be the signature of getsize:

int getsize();
Edit: in fact, I would rewrite the class like this:

1
2
3
4
5
6
7
8
9
10
class stack
{
public:
    int getsize();
    void push(int);
    int pop();
private:
    int a[10];
    int top = 0;
};
Last edited on
Ltp wrote:
Its true. But how can I get? Yet I've initialized the array size as 10.


You need to determine whether getSize should be getting the total size of the stack the number of items that are currently populated.

Both are pretty simple to do.
Last edited on
@iHutch105
Still confusing..
You have a stack, which you've given a maximum size.

Whenever something is pushed to the stack, the top member is incremented. When pushing to the stack, the size should be checked to make sure there's no overflow.

So, you need to determine what you want getSize to tell you. Do you want it to tell you the maximum size of the stack or the number of elements currently populated?

If it's the former, it's as simple as returning the value to use to create the stack in the first place. If it's the latter, it should be the value currently stored in top, provided that pushing and popping is working as expected.
Last edited on
Topic archived. No new replies allowed.