00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include "stack.h"
00004
00005 Stack *stCreate(){
00006 Stack *st = (Stack*)malloc(sizeof(Stack));
00007
00008 st->elements = listCreate();
00009 st->destroyFunction = NULL;
00010 return st;
00011 }
00012
00013 void stDestroy(Stack **stackAddress, StDestroyMode mode){
00014 Stack *st = stackAddress[0];
00015 if (mode == ST_DM_KEEP){
00016 listDestroy(&st->elements, DBLIST_DM_KEEP);
00017 }
00018 else {
00019 void *object;
00020 listPrepare(st->elements, DBLIST_TM_FROM_HEAD);
00021 while (listGetNext(st->elements, &object) != -1){
00022 if (st->destroyFunction == NULL){
00023 free(object);
00024 }
00025 else {
00026 st->destroyFunction((StackP)st, object);
00027 }
00028 }
00029 }
00030 free(st);
00031 stackAddress[0] = NULL;
00032 }
00033
00034 int stPush(Stack *st, void *object){
00035 return listInsertTail(st->elements, object);
00036 }
00037
00038 int stPop(Stack *st, void **objectAddress){
00039 return listRemoveTail(st->elements, objectAddress);
00040 }
00041
00042 int stGetTop(Stack *st, void **objectAddress){
00043 return listGetTail(st->elements, objectAddress);
00044 }
00045
00046 int stNumElements(Stack *st){
00047 return st->elements->numElements;
00048 }
00049
00050 int stRegisterDestroyFunction(Stack *st, StDestroyFunction *destroyFunction){
00051 st->destroyFunction = destroyFunction;
00052 return 1;
00053 }
00054