OurFamily Page
OurFamily Page

Reputation: 233

Assign values of a stack to another stack

I am working on this assignment.

I need to create a temporary stack without initializing it.

Then push the items of stack 1 into this temporary stack using a while loop.

Then I need to use another (nested?) loop to walk through the temp stack and add the items from temp stack onto stack 2.

Then I need to set stack 1 and 2 equal so stack 2 remains unchanged.

Upvotes: 1

Views: 4607

Answers (1)

ccoakley
ccoakley

Reputation: 3255

Your interfaces look a bit off. Let's start there and see if that gets you over your hump.

stack.top() usually peeks at an item, but doesn't remove it. This doesn't seem useful for transfering from one stack to anther. You already have isEmptyStack() to check that the top element exists.

stack.pop() usually takes the top item from a stack. This sounds useful for transferring.

stack.push(item) places item onto the top of the stack. This sounds useful for transferring.

stack.push() just seems wrong. Push what?

Hopefully, once you implement these methods, the rest will start to make sense from the english description of the problem you provided.

Update: this is what you want:

|a  |   |     |   |   |     |   |   |     |   |c  |
|b  |   |     |b  |   |     |   |b  |     |   |b  |
|c  |   |     |c  |a  |     |c  |a  |     |   |a  |
1   tmp 2     1   tmp 2     1   tmp 2     1   tmp 2

|   |   |     |   |   |     |   |   |a
|   |b  |     |   |   |b    |   |   |b
|   |a  |c    |   |a  |c    |   |   |c
1   tmp 2     1   tmp 2     1   tmp 2 

Now, with just push, pop, and IsEmptyStack, with no assigning of stacks to one another (that sort of defeats the purpose of the assignment), can you do this?

Upvotes: 2

Related Questions