Which is the effective way to write stack program in
C.
I presume that, by "stack program", you mean a program that demonstrates
the use of a "last-in first-out" data structure.
Pointers ,structure,func tions etc............ ...
Which will be more effective
Well, you're going to need all of those, really. You can probably get away
without using structures, though, if you're only interested in a simple
demo.
The simplest way to implement a stack data structure is to decide on the
maximum number of items you'd like to stack, and then define an array with
that many members. Managing the stack is then a simple matter of keeping
an index that indicates the "current" item, and making sure that you don't
overflow the stack (try to put more into it than will fit) and don't
underflow it (try to take out something that was never put in).
This is simple because it can be done with an ordinary array definition:
This is so simple that I was just able to code it up in a few moments
(although I must admit I haven't actually compiled it!). But it is
limited, both because that it can only stack ints and because it can only
stack MAX_STACK of them. A fuller implementation of a stack would get rid
of both these limitations, using generic pointers and dynamic memory
allocation. I am guessing that you are not quite ready for such techniques
yet.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Which is the effective way to write stack program in
C.Pointers ,structure,func tions etc............ ...
Which will be more effective
Your question is so general that it's not possible to answer it
meaningfully.
If you want to write a program, the first step is to decide what it
needs to do. You mentioned a "stack program". I guess you want to
write a program that implements and uses some sort of last-in
first-out data structure, i.e., a "stack". But what do you want the
program to do? What inputs should it accept, and what output should
it produce?
Once you've decided what you want to do, make an effort to write the
program yourself. If you run into problems or have questions, *then*
we'll be glad to help.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
"beginers of c" <ssksakthi@gmai l.comwrote in message news:
Which is the effective way to write stack program in
C.Pointers ,structure,func tions etc............ ...
Which will be more effective
>
The easiest way is to declare an array of the items that you want on your
stack, and maintain an integer that tells you where the current stack top
is. You then write push() and pop() functions to manage the stack.
Um, possibly. Let's see: valid indices are 0 to... hmmm, pre-dec on pop...
yes, you're right. Not sure what I was thinking of. Oh well. Thanks.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On Oct 6, 10:14 am, Antoninus Twink <nos...@nospam. invalidwrote:
On 5 Oct 2008 at 22:29, Richard Heathfield wrote:
>
vipps...@gmail. com said:
Shouldn't that be if(stackidx < MAX_STACK)?
>
Um, possibly. Let's see: valid indices are 0 to... hmmm, pre-dec on
pop... yes, you're right. Not sure what I was thinking of. Oh well.
>
Judging by your contempt for efficiency in the past, wasting one int of
memory is likely to be the least of the overheads in any program you
write.
There's a difference between wasted memory and unused memory.
Wasted memory is when your program is using more memory that it should
be ideally using;
Unused memory is when your program is not using all the memory it has
allocated.
There's plenty reasons why one wouldn't mind wasting memory, but I
can't think of any for "not using" memory.
(note: don't confuse "not using" with reserving, as many protocols do
that, especially networking ones)
On Oct 6, 10:14 am, Antoninus Twink <nos...@nospam. invalidwrote:
>On 5 Oct 2008 at 22:29, Richard Heathfield wrote:
>>
vipps...@gmail. com said:
>Shouldn't that be if(stackidx < MAX_STACK)?
>>
Um, possibly. Let's see: valid indices are 0 to... hmmm, pre-dec on
pop... yes, you're right. Not sure what I was thinking of. Oh well.
>>
>Judging by your contempt for efficiency in the past, wasting one int of
>memory is likely to be the least of the overheads in any program you
>write.
>
>
There's a difference between wasted memory and unused memory.
True enough. Bear in mind, however, that you're arguing with someone who
doesn't understand the word "efficiency ".
Wasted memory is when your program is using more memory that it should
be ideally using;
Unused memory is when your program is not using all the memory it has
allocated.
>
There's plenty reasons why one wouldn't mind wasting memory, but I
can't think of any for "not using" memory.
(note: don't confuse "not using" with reserving, as many protocols do
that, especially networking ones)
Over-allocation of buffers when you don't know in advance how much you're
going to need, to avoid going to the well too many times. Or is that what
you mean by "reserving" ?
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On Oct 6, 6:24 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
vipps...@gmail. com said:
>
There's plenty reasons why one wouldn't mind wasting memory, but I
can't think of any for "not using" memory.
(note: don't confuse "not using" with reserving, as many protocols do
that, especially networking ones)
>
Over-allocation of buffers when you don't know in advance how much you're
going to need, to avoid going to the well too many times. Or is that what
you mean by "reserving" ?
Yes. I had reserved bits in network headers in mind though.
Comment