/*
 *  stack.c
 *
 *  Created September 15, 190 by Raymie Stata
 *  (c) 1990 Raymie Stata, All Rights Reserved
 */

#include <stdio.h>
#include "stack.h"
#include "bucket.h"

#define LOCAL static
#define ENTRY /* */


LOCAL Bucket snodes = NULL;

struct stacknode {
  char *thing;
  struct stacknode *next;
};

ENTRY Stack stk_create()
{
  Stack s;
  if (! snodes) snodes = bk_create(sizeof(struct stacknode));
  s = (Stack) bk_alloc(snodes);
  s->next = NULL;
  return(s);
}

ENTRY extern int stk_destroy(s)
  Stack s;
{
  do {
    char *tmp = (char *)s; s = s->next; bk_free(snodes, tmp);
  } while(s);
  return(0);
}

ENTRY int stk_push(s, e)
  Stack s;
  char *e;
{
  Stack tmp = (Stack) bk_alloc(snodes);
  if (! tmp) return(1);
  tmp->next = s->next;  s->next = tmp;
  return(0);
}

ENTRY char *stk_pop(s, empty)
  Stack s;
  char *empty;
{
  char *element;
  Stack tmp = s->next;
  if (! tmp) return(empty);
  s = tmp->next;
  element = tmp->thing;
  bk_free(snodes, (char *)tmp);
  return(element);
}

  
