stacks.c


#include <stdio.h>
#include <stdlib.h>

/********* Belongs in a .h file, with prototypes *************/
typedef struct _tagSTACK {
	int *Body;
	int Index, Size;
	} STACK;

enum ErrCode { ERR_OK, ERR_FULL, ERR_EMPTY };

/************************************************************************************
** Name: 	StackIsFull (A macro)
** Descr:   Determines if Stack is full, or not
** Params:	x = Address of STACK structure to test
** Returns:	1(true) if STACK x is full, 0(false) if it isn't
*************************************************************************************/
#define StackIsFull(x) ((x)->Index==(x)->Size)

/************************************************************************************
** Name: 	StackIsEmpty (A macro)
** Descr:   Determines if Stack is empty, or not
** Params:	x = Address of STACK structure to test
** Returns:	1(true) if STACK x is empty, 0(false) if it isn't
*************************************************************************************/
#define StackIsEmpty(x) ((x)->Index==0)

/************************************************************/	

/************************************************************************************
** Name: 	StackCreate
** Descr:   Creates a STACK structure, able to contain 'Size' elements
** Params:	Size = Specifies how many elements the newly-created STACK needs to hold
** Returns:	NULL upon failure, otherwise the address of the new STACK structure
*************************************************************************************/
STACK *StackCreate( int Size )
{
	STACK *Stack;
	if( (Stack=calloc(1,sizeof(STACK))) == NULL )
		return( NULL );
	if( (Stack->Body=malloc( Size*sizeof(int)))==NULL) {
		free( Stack );
		return( NULL );
	}
	Stack->Size=Size;
	return( Stack );
}

/************************************************************************************
** Name: 	StackPush
** Descr:   Pushes a value onto a STACK
** Params:	Stack = The STACK structure that is to receive the value
**  		Value = The value to be pushed
** Returns:	ERR_OK upon success, or an error code upon failure
*************************************************************************************/
int StackPush( STACK *Stack, int Value )
{
	if( StackIsFull( Stack ) )
		return( ERR_FULL );
	Stack->Body[Stack->Index++] = Value;
	return(ERR_OK);
}

/************************************************************************************
** Name: 	StackPop
** Descr:   Retrieves a value from a STACK
** Params:	Stack = The STACK structure to retrieve a value from
**			Dest = Pointer to where to store the value retrieved
** Returns:	ERR_OK upon success, or an error code upon failure
*************************************************************************************/
int StackPop( STACK *Stack, int *Dest )
{
	if( StackIsEmpty( Stack ) )
		return( ERR_EMPTY );
	*Dest = Stack->Body[--Stack->Index];
	return(ERR_OK);
}

/************************************************************************************
** Name: 	StackDestroy
** Descr:   Removes a STACK structure from memory
** Params:	Stack = The STACK structure to be destroyed
** Returns:	Nothing
*************************************************************************************/
void StackDestroy( STACK *Stack )
{
	free( Stack->Body );
	free( Stack );
}

/************************************************************************************
** Name: 	main
** Descr:   Test program for STACK routines
** Params:	Nothing
** Returns:	Nothing
*************************************************************************************/
void main( void )
{
	int i;
	STACK *Stack;

	Stack = StackCreate( 3 );
	printf("Putting 1 on stack gave %d\n", StackPush( Stack, 1 ) );
	printf("Putting 2 on stack gave %d\n", StackPush( Stack, 2 ) );
	printf("Putting 3 on stack gave %d\n", StackPush( Stack, 3 ) );
	printf("Putting 4 on stack gave %d\n", StackPush( Stack, 4 ) );
	
	if( StackPop(Stack, &i ) == 0 ) printf("pop got %d\n", i );
	else printf("pop failed!\n");
	if( StackPop(Stack, &i ) == 0 ) printf("pop got %d\n", i );
	else printf("pop failed!\n");
	if( StackPop(Stack, &i ) == 0 ) printf("pop got %d\n", i );
	else printf("pop failed!\n");
	if( StackPop(Stack, &i ) == 0 ) printf("pop got %d\n", i );
	else printf("pop failed!\n");
}