Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Homework
hi..
 
i need to learn stack implementation in c.
so please suggest me.
Posted 6-Feb-13 21:13pm
Edited 6-Feb-13 22:21pm
nv330.5K
v2
Comments
nv3 at 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 at 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
good
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".
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
H.Brydon at 7-Feb-13 22:10pm
   
Great answer (+5 from me).
Edo Tzumer at 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
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2014
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