Stack sequential storage

Time:2020-12-15

Array implementation stack


Create three files: stackearray. H, stackearray. C, stackearraytest. C


stackArray.h
#ifndef STACK_ARRAY_H_
#define STACK_ARRAY_H_

#ifndef PTOI
#define PTOI( p ) ((int32_t)(int64_t)(p))
#endif
#ifndef ITOP
#define ITOP( i ) ((void *)(int64_t)(i))
#endif

#define ADT StackArray

//Function: the process of comparison between a and B
//Parameters: A, B
//Return: a > B returns a positive number, a

stackArray.c
#include 
#include 
#include 
#include "stackArray.h"


//Function: print the error message and exit the program
//Parameters: expression (error judgment expression), message (error message to be printed)
//Return: none
//Note: only triggered when expression is true
#define ERROR_EXIT( expression, message )                                    \
if( (expression) ) {                                                         \
	fprintf( stderr, "\nerror location: file = %s, func = %s, line = %d.\n", \
	                       __FILE__, __func__, __LINE__ );                   \
	fprintf( stderr, "error  message: %s%s.\n\a",                            \
	                       (message) != NULL ? (message) : __func__,         \
		                   (message) != NULL ? "" : " function error" );     \
	exit( EXIT_FAILURE );                                                    \
}

#define ADT StackArray


struct StackArray {
	int32_t capacity;
	int32_t top;
	void *array[0];
};


ADT *newStackArray( int32_t capacity ) {
	ADT *stack = NULL;

	capacity = capacity < 0 ? 512 : capacity;
	stack = malloc( sizeof(*stack) + sizeof(stack->array[0]) * capacity );
	ERROR_EXIT( !stack, NULL );
	stack->capacity = capacity;
	stack->top = 0;

	return stack;
}

void *pushStackArray( ADT *stack, void *data ) {
	ERROR_EXIT( !stack || stack->top >= stack->capacity, NULL );

	return (stack->array[stack->top++] = data);
}

void *popStackArray( ADT *stack ) {
	ERROR_EXIT( !stack || stack->top < 1, NULL );

	return stack->array[--stack->top];
}

void *peekStackArray( ADT *stack ) {
	ERROR_EXIT( !stack || stack->top < 1, NULL );

	return stack->array[stack->top - 1];
}

void *peekBottomStackArray( ADT *stack ) {
	ERROR_EXIT( !stack || stack->top < 1, NULL );

	return stack->array[0];
}

int existStackArray( ADT *stack, void *data, CompareFunc *cmp ) {
	ERROR_EXIT( !stack || !cmp, NULL );

	for( int32_t i = 0; i < stack->top; ++i ) {
		if( !cmp( stack->array[i], data ) ) {
			return 1;
		}
	}

	return 0;
}

int32_t findStackArray( ADT *stack, void *data, CompareFunc *cmp ) {
	ERROR_EXIT( !stack || !cmp, NULL );

	for( int32_t i = 0; i < stack->top; ++i ) {
		if( !cmp( stack->array[i], data ) ) {
			return i;
		}
	}

	return -1;
}

int32_t findTailStackArray( ADT *stack, void *data, CompareFunc *cmp ) {
	ERROR_EXIT( !stack || !cmp, NULL );

	for( int32_t i = stack->top - 1; i >= 0; --i ) {
		if( !cmp( stack->array[i], data ) ) {
			Return I - 0; // 0 is the bottom of the stack
		}
	}

	return -1;
}

int32_t sizeStackArray( ADT *stack ) {
	ERROR_EXIT( !stack, NULL );

	return stack->top;
}


int emptyStackArray( ADT *stack ) {
	ERROR_EXIT( !stack, NULL );

	return stack->top < 1;
}

int fullStackArray( ADT *stack ) {
	ERROR_EXIT( !stack, NULL );

	return stack->top >= stack->capacity;
}

int32_t capacityStackArray( ADT *stack ) {
	ERROR_EXIT( !stack, NULL );

	return stack->capacity;
}

void clearStackArray( ADT *stack ) {
	ERROR_EXIT( !stack, NULL );

	stack->top = 0;
}

void delStackArray( ADT **stack ) {
	ERROR_EXIT( !stack, NULL );
	free( *stack );
	*stack = NULL;
}

stackArrayTest.c
#include 
#include 
#include 
#include 
#include "stackArray.h"

//A > B returns a positive number