Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to programe a stack process for first in and last out!

 
sue song
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am a beginner, and now there is one question which I cannot get any clue of it. Could any one have some hint of it? Thanks very much in advance!
An integer stack can be thought of as an array of integers which behaves according to First in last out principle; the first integer put to the stack will be the last one which gets out of the stack. Write an object oriented program which simulates the way numbers are put to a stack and the way they are retrieved out of the stack of 10 numbers.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sue,
Welcome to JavaRanch!
Here's a basic outline of what you need to do:
1) Define a class IntegerStack .
2) Give IntegerStack a member variable that is an int array.
3) Give IntegerStack a member variable used as an index to "point" to the top of the stack.
4) define a method push(int) which adds an integer to the stack.
5) define a method pop() which removes the integer on the top of the stack and returns it.
Why not see how far you can get with this, then come back to this thread and ask more specific questions if you get stuck.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to read your topic line about 10 times ... I've usually heard FIFO First In First Out and LIFO Last In First Out. I have seen FILO First In Last Out but long ago and not very often. It should be same as LIFO, yes? Puts me in mind of those parking lots in New York City where you see your car way back in the corner with dozens of others in the way. First In, Last Out!
Ernest's API suggestions are perfect. And here's a good exercise in "information hiding". Do you want to use his "array" suggestion for storing the data? Or do you have another idea? The API doesn't change so your decision is nicely hidden.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic