I then added s.push("n"); and s.push("m"); giving n & m in the stack1.... is this about right so far?

thanks

October 9th, 2012, 12:16 PM

KevinWorkman

Re: Java Stacks question

Quote:

Originally Posted by sim18

It created two stacks s & s2.

I then added s.push("n"); and s.push("m"); giving n & m in the stack1.... is this about right so far?

thanks

Seems reasonable to me, but really, the best way to test is to simply let the code do the talking. It doesn't matter what we think, just what the program does. Try stepping through it with a debugger or at least adding some print statements to figure out exactly what's going on, if you aren't sure.

October 9th, 2012, 12:23 PM

sim18

Re: Java Stacks question

Thanks very much for the replies :)

I have added print statements and I can see exactly what is going on!

If I was going to take this one step further, (given integer 'n') and set j to the nth element of the stack, leaving the stack without its top 'n' elements.

All i have trouble with is getting my head around the wording and once that's done like you said I can start doing the code to check its right.

set j to the nth element of the stack... would I chose what the nth element is, for example I would set j to the 4th element in the stack, and get rid of the top 3 elements, or am i reading this wrong?

October 9th, 2012, 12:54 PM

KevinWorkman

Re: Java Stacks question

Quote:

Originally Posted by sim18

Thanks very much for the replies :)

I have added print statements and I can see exactly what is going on!

If I was going to take this one step further, (given integer 'n') and set j to the nth element of the stack, leaving the stack without its top 'n' elements.

All i have trouble with is getting my head around the wording and once that's done like you said I can start doing the code to check its right.

set j to the nth element of the stack... would I chose what the nth element is, for example I would set j to the 4th element in the stack, and get rid of the top 3 elements, or am i reading this wrong?

Sometimes the best way to figure that part out is to draw some examples. So say we have this as a stack:

{1, 2, 3, 4, 5}

...and n = 3. What should happen?

Well, first off, what is the 3rd (nth) element of the stack? In this example it's easy because it's 3. So what does it mean to set j equal to 3 and leave the stack without its 3 top values?

October 9th, 2012, 01:10 PM

sim18

Re: Java Stacks question

{1, 2, 3, 4, 5}

n = 3

nth element = 3rd element in stack, which is 3.

So you would pop() three times leaving {4,5}.

If it was j it would be exactly the same then? i.e

{l,m,j,n,o} n = 3

nth element = 3rd element in stack, which is j.
So you would pop() three times leaving {n,o}.

OR, would 'j' not be there to start with, because it says set j to the nth (3rd) element of the stack. {l,m,n,o}

Therefore I would have to do what I did before and use stack 2 to pop the first two elements, push 'j' into the stack, push the others from stack 2, then pop removing top n elements (3)

Thanks again for the help

October 9th, 2012, 01:44 PM

KevinWorkman

Re: Java Stacks question

That's really a question for your instructor, but the way I take it, the j in the question is just a temp variable name and has nothing to do with any 'j' values that happen to be in the stack. But I suppose I could be reading it wrong, so I'd recommend asking your instructor for clarification or some more examples.