• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class stack

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by peterx peter:

Hi!
I have problem with class Stack.
When and why should we use class Stack(FIRST IN & LAST OUT).


A stack is Last In, First Out.
You may ask why we use a screwdriver when a hammer will do the same job (i.e. putting a piece of metal into a piece of wood). The answer is: using the right tool for the job makes thing much easier.
There are certain problems which lend themselves to being solved with a stack. For example, the order in which a method is called by other methods. The most recent invoker is at the top of the stack and the first call (say, main(String args[])) is at the bottom. If you get an exception you can print out a "stack trace" of all the methods up to the one which threw the exception. Take a guess how that information is stored in the VM.
As for Vector, it's a more general kind of data structucture. It's like an array which can grow automatically. It keeps an order but one can access any element (as opposed to a stack, where you can only get the first element).
Interesting fact: If you are ever looking for a queue (First in, first out), look no further than Vector. Elements added to the Vector get added at the end and it has a method, firstElement(), which returns the first element in the Vector.
[ February 28, 2005: Message edited by: Joe Ess ]
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stacks also lend themselves to processing math functions and evaluating order of operations (compiling code).

You can evaluate large math functions with two stacks fairly efficiently and fairly simply. Look up 'reverse polish notation' (I think that's what it's called).

With that you can write a function that can process (4+5)*8^3-8 almost as easily as it would handle 2+2.
reply
    Bookmark Topic Watch Topic
  • New Topic