Is my professor asking me to draw the stack? Does he want me to draw it in action? I feel stupid but it’s not like anyone ever told me! Thank you for your help.
Wow you guys are quick. Thank you already. The complete question is: Consider two stacks, each of size n (i.e., each one can hold a maximum of n elements). If the sum of the number of elements in the two stacks is n, then any additional PUSH operation should result in an overflow error. (Note: Your implementation should take care of the fact that elements
should POP opposite to the order in which they are PUSHed).
***I’m not asking for the answer I’m just wondering… what do you think he’s asking me to do? Cause he still hasn’t answered my email and I need it done by midnight.
“Implement” generally means write, pure and simple. Your educator wants you to write code that can do what the assignment says.
Stacks of fixed size (
n) can be easily implemented as an array with a current stack depth but you have an extra twist to your assignment inasmuch as you’re only alowed to havenelements on both stacks combined rather than each stack.I would implement it as follows (pseudo-code only since it’s homework and, in any case, you haven’t specified a language):
The variables
stack1,stack2,stack1sz,stack2szandmaxszshould be declared in such a way that they survive in between function calls (i.e., not locals). All others are transitory.If you have troubles converting that to a specific language, leave a comment and I’ll provide pointers on what to look for.