/**************************************
/* Dynamic Stack Model 1.0
/* Kangqiao 2010-07-07 16:38
/* My E-mail:[email protected]
/* My Blog:http://hi.baidu.com/zunhuakq
**************************************/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* #define isempty(s) ((s)==NULL) */
typedef int __type__; /* you can define the __type__ another type */
typedef char bool; /* because C language doesn't have the boolean type,except C++ */
typedef struct _stack_item
{
__type__ value;
struct _stack_item *last;
}stack_item;
typedef stack_item *stack; /* please notice the type stack is a pointer type itself */
void push(stack *s,__type__ value) /* the "stack *s" mean "stack_item **s" */
{
stack_item *tmp;
tmp=(stack_item *)malloc(sizeof(stack_item));
tmp->value=value;
tmp->last=*s;
*s=tmp;
}
bool pop(stack *s,__type__ *var)
{
stack_item *tmp;
tmp=*s;
if(tmp)
{
*var=tmp->value;
*s=(*s)->last;
free(tmp);
return 1;
}
else return 0;
}
void empty(stack *s)
{
stack_item *tmp;
tmp=*s;
while(tmp)
{
*s=(*s)->last;
free(tmp);
tmp=*s;
}
}
/* test print */
void prt(stack s)
/* only a test function,it will print the data in the stack from the top to the bottom,pay attention to the order! */
{
/* puts("***********************"); */
while(s)
{
printf("%d\n",s->value);
s=s->last;
}
}
int main(int argc,char *argv[])
{
/* stack s=NULL;
int tmp;
puts("Stack test!");
push(&s,10);
push(&s,20);
push(&s,30);
prt(s);
pop(&s,&tmp);
push(&s,40);
printf("%d\n",tmp);
pop(&s,&tmp);
printf("%d\n",tmp);
empty(&s);
prt(s);
printf("%d\n",isempty(s));
printf("%d\n",pop(&s,&tmp));
printf("%d\n",tmp);
getch();
*/
return 0;
}