Created
September 28, 2015 11:44
-
-
Save hiepph/79b8fd735f5cab8c2fd8 to your computer and use it in GitHub Desktop.
Stack Implementation using Array
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_array.h" | |
void Initialize() | |
{ | |
top = 0; | |
} | |
int Empty() | |
{ | |
return top == 0; | |
} | |
int Full() | |
{ | |
return top == MAX_STACK_SIZE; | |
} | |
void Push(stack_t stack, data_t data) | |
{ | |
if(Full()) { | |
printf("Stack Overflow.\n"); | |
} else { | |
stack[top++] = data; | |
} | |
} | |
data_t Pop(stack_t stack) | |
{ | |
if(Empty()) { | |
printf("Stack Underflow.\n"); | |
exit(0); | |
} else { | |
return 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_ARRAY_H | |
#define STACK_ARRAY_H | |
#define MAX_ARRAY_SIZE ... | |
typedef ... data_t; | |
// define stack_t: array_type has element data_t | |
typedef data_t stack_t[MAX_STACK_SIZE]; | |
int top; | |
void Initialize(); | |
int Empty(); | |
int Full(); | |
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