Hello friends,
I have have found lots of ways to implement a stack using different data structures. But i want to what us the best way (best means code understandability , performance) to implement a stack!!
Share ur thought so i can reach to any final answer.
Different ways ->1. using Link-list
->2. using Array
->3. using pointers.

Also if u know any other method then plz share.

Thanks

Recommended Answers

All 6 Replies

From your definition of "best", an array seems the obvious choice. The code is trivially easy, and the the result has very good performance.

The down side of an array is that it is always allocated in memory. So it wastes memory when the stack is unused, and cannot be expanded when you reach capacity.


The bottom line is there is no single "best" answer which works in all situations. You have to consider the whole and make a design choice.

salem, if u are asked to implement a stack then what u prefer?

if u are asked to implement a stack then what u prefer?

I'd go for something like this :)
(a stack based on a linked list)

Friend u are suppose to give reason also why will u prefer ur approach?
thnx for reply

Friend u are suppose to give reason also why will u prefer ur approach?

Because you can dynamically expand it and because it doesn't use more memory than needed :)
(and when you pop elements, then you can free up the memory that was being used to store that element)

But the downside is that it's more difficult to implement, like Salem has already mentioned, you'll have to choose the one which fits the best in the type of application you want to write, for example, a stack implementation based on an array is also perfectly possible, it's even easier to implement, but you cannot dynamically expand it and when you don't fill up the whole array/stack, you're wasting some memory as well :)

So if you only want to store a fixed number of records, then I would rather go for the implementation based on arrays...

> salem, if u are asked to implement a stack then what u prefer?
Did you read my post?

I don't "prefer" any, I use the one which makes most sense in the circumstances.

If you get stuck in some kind of "I always do it this way" mentality, you're going to make some pretty bone-headed design decisions at some point.

commented: troof +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.