#include <stdio.h>


int	*array;
int	t = 0;
int array_size = 10;

void createStack( void ) {
  array = (int *) calloc(array_size, sizeof(int));
}

void enlargeArray( void ) {
  // use a very simple and inefficient alg to increase the size
  int newSize = array_size*2;
  int *tmpArray;
  int i=0;

  // create the new array
  tmpArray = (int *) calloc(newSize, sizeof(int) ); 

  for (i=0; i<array_size; i++) {
    tmpArray[i] = array[i];
  }

  // now dealloc the old array and put the pointer in the right place
  free(array);
  array = tmpArray;
  array_size = newSize;
}

void push( int value ){
  if ( t < array_size ) {
      array[ t ] = value;
  } else {
    // enlarge the array
    enlargeArray();
  }
  t += 1;
}


void pop(){
  if ( !is_empty() ) {
    t -= 1;
  }
}

int top() {

  int returnValue = -1;

  if ( !is_empty() ) {
    returnValue = array[ t-1 ];
  } else {
    printf("Error: you just tried to look at an empty stack!\n");
  }
  return returnValue;
}

int is_empty(){
  return t == 0;
}

void deleteStack( void ) {
  free(array);
}

void printStack( void ) {
  int i=0;
  for ( ; i<array_size; i++) {
    printf("%d: %d\n", i, array[i]);
  }
}

