C++ Stack Usage

Started by
14 comments, last by load_bitmap_file 19 years, 9 months ago
Hello. Could someone explain why/when to use stacks as data structures? All the links I got when I googled only dealt with how stacks are used by compilers. I think I understand how they work, but given the stack functions, I can't see why stacks would be used over a list or a deque. Secondly, my book tells me stacks use deques by default but can be made to use vectors or lists or some other container instead. Again, I don't see why. The only operations that occur on stacks are at the top end so deques seem to be the only logical choice..
Advertisement
Stack only is FILO.
Cause list and vector can be FIFO, FILO, LILO and LIFO.
So it can instead stack..
I think so.
A Stack is FILO (first in last out) or LIFO (last in First out) they are the same thing.
A Queue is LILO or FIFO (same thing).

You would use a stack whenver you need to reverse order is one use for it, other uses is whenver you see fit because it's not ONLY for a list of uses because any other datastructure can be used for several reasons.

A Stack example

Stack|
Add "A" to stack
Stack|A
Add "Q" to stack
Stack|AQ
Remove an object from stack
Stack|A (returned Q)

A Queue example

Queue-|
Add "A" to Queue
Queue-A|
Add "Q" to Queue
Queue-AQ|
Remove an object from Queue
Queue-Q| (returned A)

A queue is like a line ... first come first serve a crowded elevator... while a stack is like a crowded elvator ... first people in get pushed toward the back and the last in is closest to the door.
God bless-Gryfang
Yes, I know stacks are FILO but for the reverse order access, vectors lists, and deques have this functionality as well (pop_back(), push_back()). Why use stacks? Maybe an example could clarify things a bit.
I said if you needed to reverse the order of something it's mindless. Also if you want to make a generic temporary holding place for a specific type of data you don't have to make extra varables on the fly with different levels of scope you just have to a generic type global stack and push and pop. As for lower level programming it's insanely useful, and if you don't know what for then you probably haven't programmed a lot in Assembler. As with everything else there are several different uses for any data structure, and several different ways to get around even using a data structure. If you have no use for a stack then don't use it. If after a while of programming you see a need for a stack, then use it.

The biggest use I personally have for using a stack is reverseing the order of information.
God bless-Gryfang
Do you mean accessing data in reverse or "flip-flopping" all the elements in an array? Either way, the other standard containers have reverse iterators and the reverse() function to handle those operations.

Also, I'm not sure about using a global stack. Wouldn't you need a different stack for every data type you want? And afterwards wouldn't all the elements have to be cleared out and whatnot?

I've never used assembler so I don't know about that aspect of stacks. That would explain all the compiler-related stack stuff I found though.
You use std::stack when you want a stack. Its just a conceptual thing AFAICT, and really I'm not sure why they implemented some data types but not others (such as making map vs a binary tree - they might be the same internally, but the exposed interfaces would be quite different)

-Extrarius
Well You wouldn't need to make a stack for each data type, just one they all inherit from (like Object class in java).

As for the reverse iterator ... that's fine, you may not need to use a stack. But if you don't whenever, it's converted into a stack whenever it's put into the assembler level.

I do agree that a lot of uses a stacks in higher level languages is reduced but It still gets used even if you don't.

I don't know if that's what you want to hear, :) but i hope it helped.
God bless-Gryfang
a stack is an abstract data type (ADT), note the word "abstract" it can be represented by almost infinite number of ways.

Users of an ADT shouldn't need to care how it's representated they just wont to use the operations of a stack which is mainly push & pop operations. So thats why it's there it allows to view a container as stack where it logically makes sense to have that view.

The methods of push_back/front & pop_back/front in some of the other containers in STL such as list isn't normal operations of a list ADT there just convenience methods for the user so you may not see these operations in other container libraries for lists.

When do you use a stack? well when its required in your solution to a problem. Which would come up from the concepts in your problem domain.

Say you had a problem where you had to make a program that simulated the task of manually cleaning the dishes. Your work area has a sink, one side has a "stack" of dirty dishes initially the other is where you "stack" the clean ones, you can only clean a plate one at time.

If you look at the problem do you think a list of dishes sounds wright to represent the data in your program? not really a stack of dishes is more logical. Don't get me wrong you can use a list if you really wonted it to but doesn't seem like a good abstraction if you know what i mean.
That's a good example thanks snk.
I hate not being able to think up an example is a pain sometimes.
As he said it's abstract not concrete. In fast you can make a stack out of a list, or array, or 50 variables.
God bless-Gryfang

This topic is closed to new replies.

Advertisement