Click here to Skip to main content
11,931,104 members (59,818 online)
Rate this:
Please Sign up or sign in to vote.
See more: C

i need to learn stack implementation in c.
so please suggest me.
Posted 6-Feb-13 21:13pm
Edited 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 | Mobile
Web03 | 2.8.151126.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2015
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