Skip to content

Instantly share code, notes, and snippets.

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