Click here to Skip to main content
13,402,795 members (39,354 online)
Rate this:
Please Sign up or sign in to vote.
See more:

i need to learn stack implementation in c.
so please suggest me.
Posted 6-Feb-13 21:13pm
Updated 6-Feb-13 22:21pm
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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".
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

H.Brydon 7-Feb-13 22:10pm
Great answer (+5 from me).
Edo Tzumer 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.1802120.2 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100