/* The basic stack routines. */

#include "polish.h"

void init_stack(stack *stk)
{
   stk -> top = NULL;
}

node *init_node(node *d)
{

  if ( d==NULL ) 
    d=malloc(sizeof(node));

  d->op=' ';
  d->val =0;
  return d;
}

stack *push(node d, stack *stk)
{ 
   elem   *p;

   p = malloc(sizeof(elem));
   p -> d = d;
   p -> next = stk -> top;
   stk -> top = p;
   return stk;
}

node pop(stack *stk)
{ 
   node d;
   elem *p;
   if ( stk->top != NULL ) {
     d = stk -> top -> d;
     p = stk -> top;
     stk -> top = stk -> top -> next;
     free(p);
   }
   else {
     d.val = 0;
     d.op=' ';
   }
   return d;
}


void print_elements( elem *cur )
{
  if ( cur != NULL ) {
    if ( !isspace(cur->d.op) ) 
      printf("op: %c\n", cur->d.op);
    else
      printf("value %f\n", cur->d.val);
    print_elements(cur->next);
  }
	     
}


void printstack(stack *stk)
{
  print_elements( stk->top);
}


int empty(const stack *stk)
{
  if ( stk->top == NULL )
    return 1;
  else
    return 0;
}

