Skip to content

Instantly share code, notes, and snippets.

@hiepph
Created September 28, 2015 11:44
Show Gist options
  • Save hiepph/79b8fd735f5cab8c2fd8 to your computer and use it in GitHub Desktop.
Save hiepph/79b8fd735f5cab8c2fd8 to your computer and use it in GitHub Desktop.
Stack Implementation using Array
#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];
}
}
#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