拆分:
// 错误处理宏定义
#define ERROR_CHECK(condition, msg) \
do { \
if (condition) { \
printf("Error: %s\n", msg); \
exit(1); \
} \
} while(0)
typedef int ElementType;
typedef struct node_s {
ElementType data;
struct node_s* next;
}StackFrame;
typedef struct {
StackFrame* top;
}LinkedStack;
- 创建链式栈
// 创建链式栈
LinkedStack* stack_create() {
// calloc免去初始化操作
return calloc(1, sizeof(LinkedStack));
}
- 销毁链式栈
// 销毁链式栈:主要是销毁链表
void stack_destroy(LinkedStack* stack) {
StackFrame* curr = stack->top;
while (curr != NULL) {
StackFrame* tmp = curr->next;
free(curr);
curr = tmp;
}
free(stack);
}
- 判空
// 判空
bool is_empty(LinkedStack* stack) {
return stack->top == NULL;
}
- 入栈
// 入栈
void stack_push(LinkedStack* stack, ElementType data) {
// 单链表头插法的变体
StackFrame* new_frame = malloc(sizeof(StackFrame));
ERROR_CHECK(new_frame == NULL, "malloc failed in stack_push.");
new_frame->data = data;
new_frame->next = stack->top;
stack->top = new_frame;
}
- 出栈
// 出栈并返回栈顶元素
ElementType stack_pop(LinkedStack* stack) {
ERROR_CHECK(is_empty(stack), "stack is empty.");
StackFrame* curr = stack->top;
ElementType data = curr->data;
stack->top = curr->next;
free(curr);
return data;
}
- 访问栈顶
// 访问栈顶元素
ElementType stack_peek(LinkedStack* stack) {
ERROR_CHECK(is_empty(stack), "stack is empty.");
return stack->top->data;
}
整合:
linked_stack.h
#ifndef LINKED_STACK_H
#define LINKED_STACK_H
#define _CRT_SECURE_NO_WARNINGS
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// 错误处理宏定义
#define ERROR_CHECK(condition, msg) \
do { \
if (condition) { \
printf("Error: %s\n", msg); \
exit(1); \
} \
} while(0)
typedef int ElementType;
typedef struct node_s {
ElementType data;
struct node_s* next;
}StackFrame;
typedef struct {
StackFrame* top;
}LinkedStack;
// 创建链式栈
LinkedStack* stack_create();
// 销毁链式栈
void stack_destroy(LinkedStack* stack);
// 判空
bool is_empty(LinkedStack* stack);
// 入栈
void stack_push(LinkedStack* stack, ElementType data);
// 出栈并返回栈顶元素
ElementType stack_pop(LinkedStack* stack);
// 访问栈顶元素
ElementType stack_peek(LinkedStack* stack);
#endif // !LINKED_STACK_H
linked_stack.c
#include "linked_stack.h"
// 创建链式栈
LinkedStack* stack_create() {
// calloc免去初始化操作
return calloc(1, sizeof(LinkedStack));
}
// 销毁链式栈:主要是销毁链表
void stack_destroy(LinkedStack* stack) {
StackFrame* curr = stack->top;
while (curr != NULL) {
StackFrame* tmp = curr->next;
free(curr);
curr = tmp;
}
free(stack);
}
// 判空
bool is_empty(LinkedStack* stack) {
return stack->top == NULL;
}
// 入栈
void stack_push(LinkedStack* stack, ElementType data) {
// 单链表头插法的变体
StackFrame* new_frame = malloc(sizeof(StackFrame));
ERROR_CHECK(new_frame == NULL, "malloc failed in stack_push.");
new_frame->data = data;
new_frame->next = stack->top;
stack->top = new_frame;
}
// 出栈并返回栈顶元素
ElementType stack_pop(LinkedStack* stack) {
ERROR_CHECK(is_empty(stack), "stack is empty.");
StackFrame* curr = stack->top;
ElementType data = curr->data;
stack->top = curr->next;
free(curr);
return data;
}
// 访问栈顶元素
ElementType stack_peek(LinkedStack* stack) {
ERROR_CHECK(is_empty(stack), "stack is empty.");
return stack->top->data;
}
测试代码main.c
#include "stack.h"
/*
链式栈
// 创建链式栈
LinkedStack* stack_create();
// 销毁链式栈
void stack_destroy(LinkedStack* stack);
// 判空
bool is_empty(LinkedStack* stack);
// 入栈
void stack_push(LinkedStack* stack, ElementType data);
// 出栈并返回栈顶元素
ElementType stack_pop(LinkedStack* stack);
// 访问栈顶元素
ElementType stack_peek(LinkedStack* stack);
*/
int main(void) {
// 测试:创建一个链式栈并压入整数1至5作元素
LinkedStack* LS = stack_create();
for (int i = 0; i < 5; i++) {
stack_push(LS, i + 1);
}
// 测试:出栈
ElementType tmp = stack_pop(LS);
printf("%d\n", tmp);
// 测试:访问栈顶元素
tmp = stack_peek(LS);
printf("%d\n", tmp);
// 测试:过度出栈
for (int i = 0; i < 5; i ++) {
stack_pop(LS);
}
// 测试:销毁链式栈
stack_destroy(LS);
return 0;
}