创建3个文件: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
// 功能: a与b的比较过程.
// 参数: a, b.
// 返回: a>b返回正数, a
stackLinked.c
#include
#include
#include
#include "stackLinked.h"
// 功能: 打印错误信息后就错误退出程序.
// 参数: expression(错误判断表达式), message(需打印的错误信息).
// 返回: 无.
// 注意: 当表达式 expression 为真时, 才触发.
#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; // 指向栈底.
Node *next; // 指向栈顶.
};
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指针法反转链表.
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指针法再次反转链表, 恢复原顺序.
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 ) { // 三指针法反转链表.
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返回正数, a