Stack chain storage

Time:2020-11-27

Linked list implementation stack


Create three files: stacklinked. H, stacklinked. C, stacklinkedtest. C


stackLinked.h
#ifndef STACK_LINKED_H_
#define STACK_LINKED_H_

#ifdef __GNUC__
	#define DEPRECATED __attribute__( (deprecated) )
#elif defined(_MSC_VER)
	#define DEPRECATED __declspec( deprecated )
#else
	#define DEPRECATED
#endif

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

#define ADT StackLinked

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

stackLinked.c
#include 
#include 
#include 
#include "stackLinked.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 StackLinked


typedef struct NodeStackLinked {
	void *data;
	struct NodeStackLinked *next;
} Node;

struct StackLinked {
	int32_t size;
	Node * prev; // points to the bottom of the stack
	Node * next; // points to the top of the stack
};


ADT *newStackLinked( void ) {
	ADT *stack = NULL;

	stack = calloc( sizeof(*stack), 1 );
	ERROR_EXIT( !stack, NULL );

	return stack;
}

void *pushStackLinked( ADT *stack, void *data ) {
	Node *node = NULL;

	ERROR_EXIT( !stack, NULL );
	node = malloc( sizeof(*node) );
	ERROR_EXIT( !node, NULL );
	node->data = data;
	node->next = stack->next;
	stack->prev = !stack->prev ? node : stack->prev;
	stack->next = node;
	++stack->size;

	return data;
}

void *popStackLinked( ADT *stack ) {
	void *data = NULL;
	Node *node = NULL;

	ERROR_EXIT( !stack || stack->size < 1, NULL );
	node = stack->next;
	stack->prev = node != stack->prev ? stack->prev : NULL;
	stack->next = node->next;
	--stack->size;
	data = node->data;
	free( node );

	return data;
}

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

	return stack->next->data;
}

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

	return stack->prev->data;
}

int existStackLinked( ADT *stack, void *data, CompareFunc *cmp ) {
	Node *n = NULL;

	ERROR_EXIT( !stack || !cmp, NULL );
	for( n = stack->next; n != NULL; n = n->next ) {
		if( !cmp( n->data, data ) ) {
			return 1;
		}
	}

	return 0;
}

int32_t findStackLinked( ADT *stack, void *data, CompareFunc *cmp ) {
	Node *n = NULL;
	int32_t i = 0;

	ERROR_EXIT( !stack || !cmp, NULL );
	for( n = stack->next; n != NULL; n = n->next ) {
		if( !cmp( n->data, data ) ) {
			return i;
		}
		++i;
	}

	return -1;
}

int32_t findTailStackLinked( ADT *stack, void *data, CompareFunc *cmp ) {
	Node *p1 = NULL, *p2 = NULL, *p3 = NULL;
	int32_t ret = -1, i = 0;

	ERROR_EXIT( !stack || !cmp, NULL );
	//3 pointer reverse linked list
	for( p1 = NULL, p2 = stack->next; p2 != NULL; p2 = p3 ) {
		p3 = p2->next;
		p2->next = p1;
		p1 = p2;
	}
	for( p3 = p1; p3 != NULL; p3 = p3->next ) {
		if( !cmp( p3->data, data ) ) {
			ret = i;
			break;
		}
		++i;
	}
	//3 pointer method to reverse the list again, restore the original order
	for( p2 = p1, p1 = NULL; p2 != NULL; p2 = p3 ) {
		p3 = p2->next;
		p2->next = p1;
		p1 = p2;
	}

	return ret;
}

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

	return stack->size;
}

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

	return stack->size < 1;
}

void reversalStackLinked( ADT *stack ) {
	Node *p1 = NULL, *p2 = NULL, *p3 = NULL;

	ERROR_EXIT( !stack, NULL );
	stack->prev = stack->next;
	For (P1 = null, P2 = stack - > next; P2! = null; P2 = P3) {// three finger needling inverted linked list
		p3 = p2->next;
		p2->next = p1;
		p1 = p2;
	}
	stack->next = p1;
}

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

	return 0;
}

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

	return INT32_MAX;
}

void clearStackLinked( ADT *stack ) {
	Node *current = NULL, *next = NULL;

	ERROR_EXIT( !stack, NULL );
	for( current = stack->next; current != NULL; current = next ) {
		next = current->next;
		free( current );
	}
	stack->size = 0;
}

void delStackLinked( ADT **stack ) {
	Node *current = NULL, *next = NULL;

	ERROR_EXIT( !stack, NULL );
	if( !stack ) {
		return;
	}
	for( current = stack[0]->next; current != NULL; current = next ) {
		next = current->next;
		free( current );
	}
	free( *stack );
	*stack = NULL;
}

stackLinkedTest.c
#include 
#include 
#include 
#include 
#include "stackLinked.h"

//A > B returns a positive number

Recommended Today

Summary of recent use of gin

Recently, a new project is developed by using gin. Some problems are encountered in the process. To sum up, as a note, I hope it can help you. Cross domain problems Middleware: func Cors() gin.HandlerFunc { return func(c *gin.Context) { //Here you can use * or the domain name you specify c.Header(“Access-Control-Allow-Origin”, “*”) //Allow header […]