view src/stack.c @ 25:2b19746a4e97

Added stack_destroy, separated programs, added swap operation stack_destroy unallocates all space that is currently being used by a given stack. I now have the rpn program and my memory test programs in separate files. The swap operation is exactly what one would expect.
author Jonathan Pevarnek <pevarnj@gmail.com>
date Wed, 16 Mar 2011 17:29:25 -0400
parents 45a80ea314ae
children c1ad124f2aaf
line wrap: on
line source

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

eltCon pop(Stack *stack)
{
	eltCon ret;
	if(stack->top == NULL) {
		ret.error = 1;
	} else {
		ret.error = 0;
		StackElt *top = stack->top;
		ret.val = top->elt;
		stack->top = top->next;
		free(top);
	}
	return ret;
}

void push(Stack *stack, eltType val)
{
	StackElt *new = malloc(sizeof(StackElt));
	new->elt = val;
	new->next = stack->top;
	stack->top = new;
}

Stack* stack_init()
{
	Stack *new = malloc(sizeof(Stack));
	new->top = NULL;
	return new;
}

void stack_destroy(Stack *stack)
{
	while(stack->top) pop(stack);
	free(stack);
}