C语言中栈数据结构的基本实现

概述

栈的特点

使用只能在一端进行插入和删除操作的特殊线性表。

按照后进先出的原则存储数据:

  • 先进入的数据被压入栈底,最后的数据在栈顶
  • 需要读取数据的时候,从栈顶开始读取数据

基本实现

栈的创建和初始化

stack.h

#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H

// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

// 栈
typedef struct stack {
    int *arr;
    int cap;
    int top;
} Stack;

// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈

#endif //C01_HELLO_CLION_STACK_H

stack.c

#include "stack.h"

// 创建栈
void newStack(Stack *stack, int cap) {
    // 给栈分配内存
    stack->arr = malloc(sizeof(int) * cap);
    // 初始化容量
    stack->cap = cap;
    // 初始化top
    stack->top = 0;
}

// 释放栈
void freeStack(Stack *stack) {
    free(stack->arr);
    stack->cap = 0;
    stack->top = 0;
}

判断栈是否满了

核心代码:

int isFullStack(Stack *stack) {
    return stack->top >= stack->cap;
}

stack.h

#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H

// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

// 栈
typedef struct stack {
    int *arr;
    int cap;
    int top;
} Stack;

// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了

#endif //C01_HELLO_CLION_STACK_H

stack.c

#include "stack.h"

// 创建栈
void newStack(Stack *stack, int cap) {
    // 给栈分配内存
    stack->arr = malloc(sizeof(int) * cap);
    // 初始化容量
    stack->cap = cap;
    // 初始化top
    stack->top = 0;
}

// 释放栈
void freeStack(Stack *stack) {
    free(stack->arr);
    stack->cap = 0;
    stack->top = 0;
}

// 判断栈是否满了
int isFullStack(Stack *stack) {
    return stack->top >= stack->cap;
}

判断栈是否为空

核心代码:

int isEmptyStack(Stack *stack) {
    return stack->top == 0;
}

stack.h

#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H

// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

// 栈
typedef struct stack {
    int *arr;
    int cap;
    int top;
} Stack;

// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了
extern int isEmptyStack(Stack *stack); // 判断栈是否为空

#endif //C01_HELLO_CLION_STACK_H

stack.c

#include "stack.h"

// 创建栈
void newStack(Stack *stack, int cap) {
    // 给栈分配内存
    stack->arr = malloc(sizeof(int) * cap);
    // 初始化容量
    stack->cap = cap;
    // 初始化top
    stack->top = 0;
}

// 释放栈
void freeStack(Stack *stack) {
    free(stack->arr);
    stack->cap = 0;
    stack->top = 0;
}

// 判断栈是否满了
int isFullStack(Stack *stack) {
    return stack->top >= stack->cap;
}

// 判断栈是否为空
int isEmptyStack(Stack *stack) {
    return stack->top == 0;
}

入栈

核心代码:

void pushStack(Stack *stack, int value) {
    if (stack == NULL) {
        return;
    }
    if (isFullStack(stack)) {
        printf("the stack is full\n");
        return;
    }
    stack->arr[stack->top] = value;
    stack->top++;
}

stack.h

#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H

// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

// 栈
typedef struct stack {
    int *arr;
    int cap;
    int top;
} Stack;

// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了
extern int isEmptyStack(Stack *stack); // 判断栈是否为空
extern void pushStack(Stack *stack, int value); // 入栈

#endif //C01_HELLO_CLION_STACK_H

stack.c

#include "stack.h"

// 创建栈
void newStack(Stack *stack, int cap) {
    // 给栈分配内存
    stack->arr = malloc(sizeof(int) * cap);
    // 初始化容量
    stack->cap = cap;
    // 初始化top
    stack->top = 0;
}

// 释放栈
void freeStack(Stack *stack) {
    free(stack->arr);
    stack->cap = 0;
    stack->top = 0;
}

// 判断栈是否满了
int isFullStack(Stack *stack) {
    return stack->top >= stack->cap;
}

// 判断栈是否为空
int isEmptyStack(Stack *stack) {
    return stack->top == 0;
}

// 入栈
void pushStack(Stack *stack, int value) {
    if (stack == NULL) {
        return;
    }
    if (isFullStack(stack)) {
        printf("the stack is full\n");
        return;
    }
    stack->arr[stack->top] = value;
    stack->top++;
}

出栈

核心代码:

int popStack(Stack *stack) {
    if (stack == NULL) {
        printf("the stack is null\n");
        return -1;
    }
    if (isEmptyStack(stack)) {
        printf("the stack is empty\n");
        return -1;
    }

    int value = stack->arr[stack->top];
    stack->top--;

    return value;
}

stack.h

#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H

// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

// 栈
typedef struct stack {
    int *arr;
    int cap;
    int top;
} Stack;

// 栈方法
extern void newStack(Stack *stack, int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了
extern int isEmptyStack(Stack *stack); // 判断栈是否为空
extern void pushStack(Stack *stack, int value); // 入栈
extern int popStack(Stack *stack); // 出栈

#endif //C01_HELLO_CLION_STACK_H

stack.c

#include "stack.h"

// 创建栈
void newStack(Stack *stack, int cap) {
    // 给栈分配内存
    stack->arr = malloc(sizeof(int) * cap);
    // 初始化容量
    stack->cap = cap;
    // 初始化top
    stack->top = 0;
}

// 释放栈
void freeStack(Stack *stack) {
    free(stack->arr);
    stack->cap = 0;
    stack->top = 0;
}

// 判断栈是否满了
int isFullStack(Stack *stack) {
    return stack->top >= stack->cap;
}

// 判断栈是否为空
int isEmptyStack(Stack *stack) {
    return stack->top == 0;
}

// 入栈
void pushStack(Stack *stack, int value) {
    if (stack == NULL) {
        return;
    }
    if (isFullStack(stack)) {
        printf("the stack is full\n");
        return;
    }
    stack->arr[stack->top] = value;
    stack->top++;
}

// 出栈
int popStack(Stack *stack) {
    if (stack == NULL) {
        printf("the stack is null\n");
        return -1;
    }
    if (isEmptyStack(stack)) {
        printf("the stack is empty\n");
        return -1;
    }

    int value = stack->arr[stack->top];
    stack->top--;

    return value;
}

创建栈方法优化

核心代码:

// 创建栈
Stack *newStack(int cap) {
    // 创建栈
    Stack *stack = malloc(sizeof(Stack));
    // 给栈数组分配内存
    stack->arr = malloc(sizeof(int) * cap);
    // 初始化容量
    stack->cap = cap;
    // 初始化top
    stack->top = 0;

    // 返回
    return stack;
}

// 释放栈
void freeStack(Stack *stack) {
    free(stack->arr);
    free(stack);
}

stack.h

#ifndef C01_HELLO_CLION_STACK_H
#define C01_HELLO_CLION_STACK_H

// 公共头文件
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

// 栈
typedef struct stack {
    int *arr;
    int cap;
    int top;
} Stack;

// 栈方法
extern Stack * newStack(int cap); // 初始化
extern void freeStack(Stack *stack); // 释放栈
extern int isFullStack(Stack *stack); // 判断栈是否满了
extern int isEmptyStack(Stack *stack); // 判断栈是否为空
extern void pushStack(Stack *stack, int value); // 入栈
extern int popStack(Stack *stack); // 出栈

#endif //C01_HELLO_CLION_STACK_H

stack.c

#include "stack.h"

// 创建栈
Stack *newStack(int cap) {
    // 创建栈
    Stack *stack = malloc(sizeof(Stack));
    // 给栈数组分配内存
    stack->arr = malloc(sizeof(int) * cap);
    // 初始化容量
    stack->cap = cap;
    // 初始化top
    stack->top = 0;

    // 返回
    return stack;
}

// 释放栈
void freeStack(Stack *stack) {
    free(stack->arr);
    free(stack);
}

// 判断栈是否满了
int isFullStack(Stack *stack) {
    return stack->top >= stack->cap;
}

// 判断栈是否为空
int isEmptyStack(Stack *stack) {
    return stack->top == 0;
}

// 入栈
void pushStack(Stack *stack, int value) {
    if (stack == NULL) {
        return;
    }
    if (isFullStack(stack)) {
        printf("the stack is full\n");
        return;
    }
    stack->arr[stack->top] = value;
    stack->top++;
}

// 出栈
int popStack(Stack *stack) {
    if (stack == NULL) {
        printf("the stack is null\n");
        return -1;
    }
    if (isEmptyStack(stack)) {
        printf("the stack is empty\n");
        return -1;
    }

    int value = stack->arr[stack->top];
    stack->top--;

    return value;
}

使用栈

main.c

#include <stdio.h>
#include "stack.h"

int main() {
    // 创建栈
    Stack *stack = newStack(3);

    // 入栈
    pushStack(stack, 11);
    pushStack(stack, 22);
    pushStack(stack, 33);
    pushStack(stack, 333); // 栈满了

    // 出栈
    printf("%d\n", popStack(stack));
    printf("%d\n", popStack(stack));
    printf("%d\n", popStack(stack));
    printf("%d\n", popStack(stack)); // 栈空了


    // 释放内存
    freeStack(stack);

    return 0;
}

输出:

the stack is full
305
33
22
the stack is empty
-1

遍历栈

main.c

#include <stdio.h>
#include "stack.h"

int main() {
    // 创建栈
    Stack *stack = newStack(3);

    // 入栈
    pushStack(stack, 11);
    pushStack(stack, 22);
    pushStack(stack, 33);

    // 遍历栈
    while (!isEmptyStack(stack)) {
        printf("%d\n", popStack(stack));
    }

    // 释放内存
    freeStack(stack);

    return 0;
}

输出:

588
33
22
  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python私教

创业不易,请打赏支持我一点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值