view src/stack.c @ 3:0aa0ad9e1cc3

Converted to work with doubles instead of ints (and other changes) Yeah, I really should have done this in seperate commits... My mistake in operations.h was fixed, the name table is now defined in operations.c All operations have been modified to start with op_ so I can identify the functions easier Functions ftoa and atof were defined (their functions are kind of obvious) Functions ftoa and itoa now return the location of the string they create iPrint was removed, it was useless The function append was created, it appends two strings
author Jonathan Pevarnek <pevarnj@gmail.com>
date Tue, 01 Mar 2011 23:33:51 -0500
parents b6182f00de82
children 25b2b501a5fa
line wrap: on
line source

#include <stack.h>
#include <std.h>

eltType pop(struct Stack *stack)
{
	if(stack->isEmpty) {
		sPrint("ERROR: STACK IS EMPTY.\n");
		return -1;
	} else if(stack->top == 0) {
		stack->isEmpty = 1;
		return stack->values[stack->top];
	} else if(stack->top < 0) {
		return -1; //TODO figure out a better way to handle errors
	} else {
		return stack->values[stack->top--];
	}
}

void push(struct Stack *stack, eltType val)
{
	if(stack->isEmpty) {
		stack->isEmpty = 0;
		stack->values[stack->top] = val;
	} else {
		stack->values[++stack->top] = val;
	}
}

void initStack(struct Stack *stack)
{
	stack->isEmpty = 1;
	stack->top = 0;
}