链式栈的基本功能实现

拆分:

// 错误处理宏定义
#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;
  1. 创建链式栈
// 创建链式栈
LinkedStack* stack_create() {
    // calloc免去初始化操作
    return calloc(1, sizeof(LinkedStack));
}
  1. 销毁链式栈
// 销毁链式栈:主要是销毁链表
void stack_destroy(LinkedStack* stack) {
    StackFrame* curr = stack->top;
    while (curr != NULL) {
        StackFrame* tmp = curr->next;
        free(curr);
        curr = tmp;
    }
    free(stack);
}
  1. 判空
// 判空
bool is_empty(LinkedStack* stack) {
    return stack->top == NULL;
}
  1. 入栈
// 入栈
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;
}
  1. 出栈
// 出栈并返回栈顶元素
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;
}
  1. 访问栈顶
// 访问栈顶元素
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值