<jo**********@post.czwrote in message
news:fc**********************************@p25g2000 hsf.googlegroups.com...
Hello,
I've came across an article at http://www.relisoft.com/Win32/olerant.html
from which is the citation below:
....>Long, long time ago, when computer languages were still in their infancy,
the wizards of C were trying to implement a stack. They
However from this excerpt I do not understand how such a stack
actually works. Could anyone please provide an explanation of the
stack being described by the author?
I've no idea about the sort of stack being described. But implemented
something similar isn't that difficult I don't think. I put together this
code containing functions push() and pop() that require no housekeeping by
the caller:
#include <stdio.h>
#include <stdlib.h>
typedef int T; /* Must specify what data type to store */
typedef struct sstackelem{
T value;
struct sstackelem *next;
} stackelem;
stackelem *stack=NULL;
int push(T x) {
stackelem *new;
new=malloc(sizeof(stackelem));
if (new==NULL) return 0; /* Fail */
new->value=x;
new->next=stack;
stack=new;
return 1; /* OK */
}
T pop(void) {
T x;
stackelem *old;
if (stack==NULL) return 0;
x=stack->value;
old=stack;
stack=stack->next;
free(old);
return x;
}
int main(void) {
T x;
#define marker -1
if (!push(marker)) exit(0);
push(100);
push(200);
push(300);
push(400);
while((x=pop())!=marker)
printf("%d\n",x);
}
--
Bartc