Sunday, September 9, 2012

Stack Operations

Stack create, push, pop, empty operations

 typedef struct _stack {
 int top;
 void* array[20];
} Stack;

Stack* createStack()  
 {  
   Stack *stack = malloc(sizeof(Stack));  
   memset(stack, 0, sizeof(Stack));  
   return stack;  
 }  
   
 void push(Stack* stack, void* elem)  
 {  
   stack->array[stack->top] = elem;  
   stack->top += 1;  
 }  
   
 void* pop(Stack* stack)  
 {  
   void* elem = stack->array[stack->top-1];  
   stack->top -= 1;  
   return elem;  
 }  

 void* top(Stack* stack)
 {
   if (stack->top == 0)
     return NULL;

   return stack->array[stack->top-1];
 }
   
 int isEmpty(Stack* stack)  
 {  
   return stack->top;  
 }  

No comments:

Post a Comment