usermode/library/common/stack.c

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 
00004 #include "stack.h"
00005 
00006 int StackNotEmpty(stk_stack * theStack) {
00007   return( theStack ? (int) theStack->top : 0);
00008 }
00009 
00010 stk_stack * StackJoin(stk_stack * stack1, stk_stack * stack2) {
00011   if (!stack1->tail) {
00012     free(stack1);
00013     return(stack2);
00014   } else {
00015     stack1->tail->next=stack2->top;
00016     stack1->tail=stack2->tail;
00017     free(stack2);
00018     return(stack1);
00019   }
00020 }
00021 
00022 stk_stack * StackCreate() {
00023   stk_stack * newStack;
00024   
00025   if (!(newStack=(stk_stack *) malloc(sizeof(stk_stack)))) {
00026     return NULL;
00027   }
00028   newStack->top=newStack->tail=NULL;
00029   return(newStack);
00030 }
00031 
00032 
00033 void StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer) {
00034   stk_stack_node * newNode;
00035 
00036   if(!theStack->top) {
00037     if (!(newNode=(stk_stack_node *) malloc(sizeof(stk_stack_node)))) {
00038       return NULL;
00039         }
00040     newNode->info=newInfoPointer;
00041     newNode->next=theStack->top;
00042     theStack->top=newNode;
00043     theStack->tail=newNode;
00044   } else {
00045     if (!(newNode=(stk_stack_node *) malloc(sizeof(stk_stack_node)))) {
00046       return NULL;
00047         }
00048     newNode->info=newInfoPointer;
00049     newNode->next=theStack->top;
00050     theStack->top=newNode;
00051   }
00052   
00053 }
00054 
00055 DATA_TYPE StackPop(stk_stack * theStack) {
00056   DATA_TYPE popInfo;
00057   stk_stack_node * oldNode;
00058 
00059   if(theStack->top) {
00060     popInfo=theStack->top->info;
00061     oldNode=theStack->top;
00062     theStack->top=theStack->top->next;
00063     free(oldNode);
00064     if (!theStack->top) theStack->tail=NULL;
00065   } else {
00066     popInfo=NULL;
00067   }
00068   return(popInfo);
00069 }
00070 
00071 void StackDestroy(stk_stack * theStack,void DestFunc(void * a)) {
00072   stk_stack_node * x=theStack->top;
00073   stk_stack_node * y;
00074 
00075   if(theStack) {
00076     while(x) {
00077       y=x->next;
00078       DestFunc(x->info);
00079       free(x);
00080       x=y;
00081     }
00082     free(theStack);
00083   }
00084 } 
00085     

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7