/*
   File:        stack.h
   Author:      Weng-Keen Wong
   Created:     Thurs Nov 23 14:39:00 EST 2000
   Description: Header for FIFO stack implementation

   Copyright (C) 2000, Weng-Keen Wong
*/

#ifndef STACK_H
#define STACK_H

#include "standard.h"
#include "ambs.h"
/******************************************************************************
  This code implements a FIFO stack using a doubly-linked list.  The purpose
  for writing this code is to have a stack routine that can do pushes and pops
  in constant time.  With dynamic arrays, either push or pop would require
  shifting all the elements over to an adjacent spot.

  Pushes prepend an element to the head of the doubly linked list while pops
  remove an element from the tail of the doubly linked list.
 *****************************************************************************/

/* 
  This structure is for a stack element ie. a node in the doubly linked list 
 */
typedef struct _stack_elem
{
  void* data;
  struct _stack_elem* prev;
  struct _stack_elem* next;
} stack_elem;

/* This structure implements a stack */
typedef struct _stack
{
  int size;
  stack_elem* head;
  stack_elem* tail;
  void (*free_data)(void* data);
  void (*fprintf_data)(FILE* s,char* m1,void *data, char*m2);
} stack;

/* Allocates space for an empty stack */
stack* mk_empty_stack(void (*free_data)(void* data),
		      void (*fprintf_data)(FILE* s, char* m1, void* data, char* m2));

/* Frees a stack and all the elements in it */
void free_stack(stack* s);

/* Pushes an element onto the stack */
void stack_push(stack* s, void* data);

/* Pops an element off the stack in a FIFO ordering */
void* stack_pop(stack* s);

/* Prints the stack */
void fprintf_stack(FILE* fp, char* m1, stack *s, char* m2);

/* Returns the number of elements on the stack */
int stack_size(stack* s);

#endif /* #ifndef STACK_H */







