I dont understand the question!!!

imagine we have 2 empty stack of integers S1 and S2.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
pushStack(S1,2);
pushStack(S1,3);
pushStack(S1,6);
pushStack(S1,9);
pushStack(S1,11);
pushStack(S1,13);
pushStack(S2,15);

while(!emptyStack(S1))
{
popStack(S1,x);
popStack(S1,x);
pushStack(S2,x);
}
Last edited on
That's not a question.

What exactly don't you understand?
the question need us to draw diagram of each after the following operations
OK, first of all, what does that variable "x" stand for in popStack(S1,x) and pushStack(S2,x), and why would the popStack function even have parameters??
Last edited on
@viliml - the first parameter is obviously the stack being popped. The second parameter is the element being popped. Conceptually, a stack's pop function can either return the element popped off the stack or discard it. While the standard stack class discards the element that is popped, not all stack implementers take this view. So, the 'x' argument to pop, while not "standard", is certainly not unexpected.

This code smells like C code written in C++. The 'x' parameter must be a reference, therefor it must be C++. But the 'S1' and 'S2' parameters make this look like C code. To be more C++ish, S1 and S2 should be objects on which member functions are called. Such as:

1
2
3
S1.pushStack(2);
...
S1.popStack(x);
Last edited on
Traditionally a stack wouldn't need to know which element to pop; it should always pop the top element.

If it returned the element that it popped it still doesn't warrant the use of a parameter.

@B031110034
the question need us to draw diagram of each after the following operations


It is not a question. It is your assignment.:)

It is me who can say: "I don't understand the question!"
Last edited on
@doug - so, the element popped is stored in the variable x?
the element popped is stored in the variable x?
Usually you should store top in x and then pop
Yes, I know, but that's what the program seems to do!
I've got a feeling that this isn't really operating as a stack should.

Hard to tell without taking a gander at those functions, though.
OK, back to the topic, B03... whatever(srry, but your name is just to freakin complicated) when you say
the question need us to draw diagram of each after the following operations
you mean that we draw a diagram of how many elements are there in each stack at each moment or what?
@vililm

Yes. I'm saying that you could implement a stack to return the popped value in x. I understand that it's not standard, and the "normal" way is to store top and then pop, but there is nothing inherently inconsistent with returning the popped value in x.

void popStack(Stack& s, Element& value);

std::stack does not do it this way. I would not do it this way. But the sample code above implies that the stack functions in question do it this way. I'm just saying it is not inconceivable for a popStack function to have these parameters. That's all.
Without seeing the function definition it's a pointless argument, really.

Whatever the implementation, I'd say it's....a little unconventional.
I would guess that x becomes the popped stack item.

It's pretty simple assignment. A stack is a LIFO structure. Last in is the first out. Like a stack of plates or something. You can put a plate on top of the stack (push), and you can remove a plate from the top (pop).

So just draw a picture of what the stack looks like after each line of code. I'm assuming that a popped item goes into x. So maybe just draw a circle labelled x, and after popStack(S1,x);, put 2 in that circle, etc.

I found this link which uses the same popStack(S1, x), but it's pseudocode.

http://books.google.com/books?id=A1UfbAxb6t8C&pg=PA186&lpg=PA186&dq=popStack(S1,+x)&source=bl&ots=S-Z34ulCxa&sig=2-Neo4zFOyntcISxkMdGqbvVnzs&hl=en&sa=X&ei=gEHbT-OrEurY2AWg7NXTCA&ved=0CGUQ6AEwAA#v=onepage&q=popStack(S1%2C%20x)&f=false
Last edited on
Here, try this out, it might clear it up a bit how to do your excat assignment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stack>

using namespace std;

stack<int> S1, S2;
int x;

void pushStack(stack<int>& S, int a)
{
	S.push(a);
	if (S==S1) cout<<"S1: "<<S.size()<<endl;
	else cout<<"S2: "<<S.size()<<endl;
}

void popStack(stack<int>& S, int& a)
{
	a=S.top();
	S.pop();
	if (S==S1) cout<<"S1: "<<S.size()<<endl;
	else cout<<"S2: "<<S.size()<<endl;
}

bool emptyStack(stack<int> S)
{
	return S.empty();
}

int main()
{
	pushStack(S1,2);
	pushStack(S1,3);
	pushStack(S1,6);
	pushStack(S1,9);
	pushStack(S1,11);
	pushStack(S1,13);
	pushStack(S2,15);
	while(!emptyStack(S1))
	{
		popStack(S1,x);
		popStack(S1,x);
		pushStack(S2,x);
		}
}


PS: Yea, I know I shouldn't use global variables like tat, but I did it just to make the main() excatly like his example.
Topic archived. No new replies allowed.