wood burning stoves*
The moose likes Beginning Java and the fly likes Usage areas for stacks and queues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Usage areas for stacks and queues" Watch "Usage areas for stacks and queues" New topic
Author

Usage areas for stacks and queues

Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Hi, I am currently trying to teach myself how to use stacks, queues, linked lists and binary trees (how to implement them myself from scratch first, that is, before I use what has already been provided for me in java.util.*).

I'm currently at the point where I can write stacks and queues and linked lists (and combinations of these), but I'm sort of going "so what?". I know that both stacks and queues are important, but I don't REALLY get it. Queues are used in transaction processing, that I can kind of see intuitively, but am less certain about stacks. I read something about their use in the handling/storage of data in registers during the running of recursive method calls, or during interrupt handling, but the whole thing sort of whooshed past over my head at a bit of a distance... What are the main usage areas of stacks in particular? Can someone dumb it down to my level? Now there's a challenge you probably hear every day...

Thanks,
Flo
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
A typical use of a stack would be an undo queue (yes, I know it sounds weird, the name "undo queue" while commonly used isn't really correct technically). Typically you'd want to undo the last action placed on the queue first.
Another stack is the callstack of an application. The last function placed on it will be executed first.
Or what about a car ferry that has only a single on/off ramp. If it's not properly loaded everyone has to drive off in reverse, the last car on will be the first car off (normally of course everyone is made to drive so they'll face forward on unloading, but some dockworkers aren't that smart).

A stack implements a LIFO mechanism, a queue a FIFO mechanism.


42
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I haven't had to use stacks very often, but sometimes you have to do recursive things that need them. I have a little file processor that lets FileA say "Include FileB" and I insert FileB at that point. FileB might say "include FileC". I always need to know what file I'm processing right now, so I have a current file variable. When I start a new nested file, I push the current file onto a stack and set the variable to the new file. When I finish one file, I pop the previous file off the stack and make it current again.

If I had made my method call itself in conventional recursion, the compiler would take care of all that - push the current state of all local variables onto the stack and give me a fresh set, then pop them all off again when I return.

Does that help or confuse more?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
Consider Paul's rocket mass heater.
 
subject: Usage areas for stacks and queues
 
Similar Threads
SCJP but no experience
post increment confusion
Data Classes - Structure and Type
I keep hearing LinkedLists, Binary Tree, etc...
Stack implementations