Click here to Skip to main content
15,884,933 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hi..

i need to learn stack implementation in c.
so please suggest me.
Posted
Updated 6-Feb-13 21:21pm
v2
Comments
nv3 7-Feb-13 3:23am    
Have you tried anything yourself yet? Show us what you have done and where you have problems. Just asking for help with no visible sign of your own effort will not get you far.
CPallini 7-Feb-13 3:57am    
If you know how a stack should work then you shouldn't have problems implementing it with a fixed size array.

The simplest way is to have an array on a fixed size, and an index of the current top. When you push items to the stack you store them in the array and increment the index. When you pop them, you do the other way around. Of course, this has the drawback it's a fixed size, no matter how many elements you have and you can also reach the maximum size and then would not be able to add more elements.

Another option is to have a linked-list, and always add and remove at the head. This way you allocate only the memory you need.

Now, I don't know what you mean by "simple way".
 
Share this answer
 
 
Share this answer
 
Comments
H.Brydon 7-Feb-13 22:10pm    
Great answer (+5 from me).
Joezer BH 10-Feb-13 1:12am    
Thank you Brydon

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900