• Post Reply Bookmark Topic Watch Topic
  • New Topic

Manipulating stacks. Is there a better way?  RSS feed

 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing my exercises with the restriction that I can use only the basic operations for stacks (push, pop, peek) and for queues(add, remove,peek)

In the first exercise I have to rearrange the stack using a queue as an a auxilary storage, so the values -3,4,6,-2 are printed -3,-2,4,6 after the moethod execution(negatives first).

In the second exercise i have to replace every value in the stack with two occurances of the same value preserving the original order, again, using queue as an auxilary.

What I am wondering is if there is a better way(under this restriciton) then the one I used(i am sure there is).

Any tips and advices would be appreciated.Thank you.
Werner.


 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by drawing some diagrams of the values on the stack and how you are going to add/remove them to the Queue. I think things will become clearer when you see them in diagram form. Are you restricted to one Queue?

In future, don't use the Stack class, which is one of Java's triumphs of bad design. Use this instead.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also try the exercise without using a Queue at all. Use two stacks. I know all about two stacks; I write Forth code, too.
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Start by drawing some diagrams of the values on the stack and how you are going to add/remove them to the Queue. I think things will become clearer when you see them in diagram form. Are you restricted to one Queue?

In future, don't use the Stack class, which is one of Java's triumphs of bad design. Use this instead.

Yes, I am restricted to only one queue.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that restricts the sorts of diagrams you can draw. If your stack starts off as in your first post you can have a diagram like thisRemember the top of the stack is conventionally written on the right, and at the bottom.

A long time ago I had to learn in anatomy that this is right ← and this is left →
Now in computer sciences I have had to learn that this is up ↓ and this is down ↑

Try duplicating the top value on the stack as the first thing you try.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Werner Holt wrote:I am doing my exercises with the restriction that I can use only the basic operations for stacks (push, pop, peek) and for queues(add, remove,peek).

I presume by the above that you mean a basic stack, which allows you to manipulate or view only the TOP item; and a basic LIFO queue (sometimes called a 'pipe'), which allows you to insert only at the beginning and remove or view only from the end.

If so, you shold be very careful if you implement on top of Deque, because it allows you to do both (and many other things besides).

Beyond that, I can't improve on the advice you've already been given....lots of diagrams.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!