Created
September 28, 2015 11:45
-
-
Save hiepph/5c8deacc69060052202a to your computer and use it in GitHub Desktop.
Stack Implementation using Structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "stack_structure.h" | |
void Initialize(stack_t *stack) | |
{ | |
stack->top = 0; | |
} | |
// Note: stack is a structure, so if I want to | |
// change it, i must point to it | |
int Empty(stack_t stack) | |
{ | |
return stack.top == 0; | |
} | |
int Full(stack_t stack) | |
{ | |
return stack.top == MAX_STACK_SIZE; | |
} | |
void Push(stack_t *stack, data_t data) | |
{ | |
if(Full(*stack)) { | |
printf("Stack Overflow.\n"); | |
} else { | |
stack->storage[stack->top++] = data; | |
} | |
} | |
int Pop(stack_t *stack) | |
{ | |
if(Empty(*stack)) { | |
printf("Stack Underflow.\n"); | |
exit(0); | |
} else { | |
return stack->storage[--stack->top]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STACK_STRUCTURE_H | |
#define STACK_STRUCTURE_H | |
#define MAX_STACK_SIZE ... | |
typedef ... data_t; | |
typedef struct { | |
data_t storage[MAX_STACK_SIZE]; | |
int top; | |
} stack_t; | |
void Initialize(stack_t stack); | |
int Empty(stack_t stack); | |
int Full(stack_t stack); | |
void Push(stack_t stack, data_t data); | |
data_t Pop(stack_t stack); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment