I am going some exercises and just want to make sure I am doing it right. It says for the following...Assume StackType is sent to int and STACK_CAPACITY is set to 5. Give the values of myTop and the contents of the array referred to by myArray in the stack s after the code segment is executed or indictate why an error occurs.

Stack s;
s.push(10);
s.push(22);
s.push(37);
s.pop();
s.pop();

I am guessing the answer is 10[6] and this is how I figured it in case I am doing it wrong. I starts with s at 5 and each push increases by one and each pop decreases by 1. Please let me know if this is not how I am supposed to do this question.

Recommended Answers

All 8 Replies

We do not know what myTop or myArray are supposed to represent.
However, it's safe to say that at the end the top (and only) element is 10, the stack size is 1 and the capacity probably will be still 5.

I did some additional reading and I think 5 is the total it can be so correct me if I am wrong but shouldnt it start with 0,1,2,3,4 with 0 at s, 1 at 10, etc

Huh? Total of what?
pop does not add any values, it removes the top value.

I figured out the last questions but I do have a question with the next one....

Here is my next question...

Stack s;
s.push(10);
s.push(9);
s.push(8);
while(!s.empty())
s.pop();

and the while (!s.empty() equation is myTop < Stack_CAPACITY - 1 so 2 < 4 so this statement can be ignored correct?

You need to express yourself more clearly. You have still not explained what myTop is supposed to be.
If you want to know what while(!s.empty())s.pop(); does: it removes all elements from the stack.

I apologize I am jus trying to understand these topics so just a little difficult to explain when I am not as familiar with the material but ill try harder

I think I have these done but not sure if they are right. As far as myTop goes I dont think I was ever given it...

1.

Stack s;

s.push(10);

s.push(22);

s.push(37);

s.pop();

s.pop();

This answer I put 10[0]

2.

Stack s;

s.push(10);

s.push(9);

s.push(8);

while(!s.empty())

s.pop();

This answer I put cannot be done because the last statement clears everything out

3.

Stack s;

for (int i = 1; i <= 6; i++)

s.push(10*i);

Cannot be done because I is multiplied by 10 and the
highest it can be is 6

4.

Stack s;

s.push(11);

i = s.top();

s.pop();

This answer I put
3[0]

Are you getting the concept of stack (data structure) wrong? You may get a clearer picture of how stack work at http://en.wikipedia.org/wiki/Stack_(data_structure)
Still, I could not figure out your example. Would you mind giving a clearer explanation?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.