C语言数据结构与算法之栈练习2:判断栈是否为空或者是否已满

练习2:判断栈是否为空或者是否已满

需求1:实现一个简单的栈结构,并实现入栈和出栈的功能,编写相关的代码进行测试。

需求2:如果栈已满,则不要再继续入栈,并提示错误信息。如果栈为空,则不要出栈,并提示错误信息。错误信息要输出到标准错误流。

需求3:编写代码,分别覆盖栈已满和栈为空的情况。

完整代码
c 复制代码
#include <stdio.h>
#include <stdlib.h>

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

Stack* newStack(int cap){
    Stack* stack = malloc(sizeof(Stack));
    stack -> arr = malloc(sizeof(int) * cap);
    stack -> cap = cap;
    stack -> top = 0;

    return stack;
}

void freeStack(Stack* stack){
    free(stack -> arr);
    stack -> cap = 0;
    stack -> top = 0;
    free(stack);
    stack = NULL;
}

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

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

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

int popStack(Stack* stack){
    if (isEmptyStack(stack)){
        fprintf(stderr, "the stack is empty\n");
        return -1;
    }
    int value = stack -> arr[stack -> top - 1];
    stack -> top --;
    return value;
}

int main(){
    Stack* stack = newStack(3);
    
    pushStack(stack, 111);
    pushStack(stack, 222);
    pushStack(stack, 333);
    pushStack(stack, 333333);

    int value;
    
    value = popStack(stack);
    printf("value=%d\n", value);
    value = popStack(stack);
    printf("value=%d\n", value);
    value = popStack(stack);
    printf("value=%d\n", value);
    value = popStack(stack);
    printf("value=%d\n", value);

    freeStack(stack);

    return 0;
}

输出结果:

bash 复制代码
the stack is full
value=333
value=222
value=111
the stack is empty
value=-1
核心代码:栈结构

定义栈类型的数据结构:typedef struct stack {

声明一个int类型的地址,用来存储栈的内容:int* arr;

声明栈的容量:int cap;

声明栈顶:int top;

给栈类型的结构体取个别名:} Stack;

c 复制代码
typedef struct stack {
    int* arr;
    int cap;
    int top;
} Stack;
核心代码:新建栈方法

定义新建栈的方法:Stack* newStack(int cap){

从内存中开辟一个空间,用来存储栈的信息:Stack* stack = malloc(sizeof(Stack));

从内存中开辟一个空间,用来存储栈数组容器的信息:stack -> arr = malloc(sizeof(int) * cap);

记录栈的容量:stack -> cap = cap;

记录栈顶:stack -> top = 0;

返回新建的栈:return stack;

c 复制代码
Stack* newStack(int cap){
    Stack* stack = malloc(sizeof(Stack));
    stack -> arr = malloc(sizeof(int) * cap);
    stack -> cap = cap;
    stack -> top = 0;

    return stack;
}
核心代码:释放栈方法

声明释放栈的方法:void freeStack(Stack* stack){

释放栈的数组容器内存:free(stack -> arr);

栈的容量归零:stack -> cap = 0;

栈顶归零:stack -> top = 0;

释放栈本身的内存:free(stack);

将栈的地址置空,防止野指针:stack = NULL;

c 复制代码
void freeStack(Stack* stack){
    free(stack -> arr);
    stack -> cap = 0;
    stack -> top = 0;
    free(stack);
    stack = NULL;
}
核心代码:判断栈是否已满方法

如果栈不是空指针,并且栈顶位置和容器容量相同,则说明栈已经满了。

正常情况下,栈顶位置应该小于栈容量。

c 复制代码
int isFullStack(Stack* stack){
    return stack != NULL && stack -> top == stack -> cap;
}
核心代码:判断栈是否为空方法

如果栈不是空指针,或者栈顶位置在0,则说明这个栈是个空栈。

c 复制代码
int isEmptyStack(Stack* stack){
    return stack != NULL && stack -> top == 0;
}
核心代码:入栈方法

声明入栈的方法:void pushStack(Stack* stack, int value){

判断栈是否已满:if (isFullStack(stack)){

将数据添加到栈顶的位置:stack -> arr[stack -> top] = value;

栈顶的位置自增:stack -> top ++;

c 复制代码
void pushStack(Stack* stack, int value){
    if (isFullStack(stack)){
        fprintf(stderr, "the stack is full\n");
        return;
    }
    stack -> arr[stack -> top] = value;
    stack -> top ++;
}
核心代码:出栈方法

声明出栈的方法:int popStack(Stack* stack){

判断栈是否为空:if (isEmptyStack(stack)){

获取栈顶的元素:int value = stack -> arr[stack -> top - 1];

栈顶自减:stack -> top --;

返回获取到的元素:return value;

c 复制代码
int popStack(Stack* stack){
    if (isEmptyStack(stack)){
        fprintf(stderr, "the stack is empty\n");
        return -1;
    }
    int value = stack -> arr[stack -> top - 1];
    stack -> top --;
    return value;
}
核心代码:测试方法

新建容量为3的栈对象:Stack* stack = newStack(3);

向栈中追加四条数据:pushStack(stack, 111);

弹出四次栈中的栈顶元素:int value = popStack(stack);

查看栈顶元素:printf("value=%d\n", value);

释放栈:freeStack(stack);

c 复制代码
int main(){
    Stack* stack = newStack(3);
    
    pushStack(stack, 111);
    pushStack(stack, 222);
    pushStack(stack, 333);
    pushStack(stack, 333333);

    int value;
    
    value = popStack(stack);
    printf("value=%d\n", value);
    value = popStack(stack);
    printf("value=%d\n", value);
    value = popStack(stack);
    printf("value=%d\n", value);
    value = popStack(stack);
    printf("value=%d\n", value);

    freeStack(stack);

    return 0;
}
相关推荐
晓蛋6 小时前
c语言指的是什么意思
c语言·编译器·编程开发·集成开发环境·程序实例
倒头就睡的小比特6 小时前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!6 小时前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
傲世仙尊7 小时前
目录即文件-Ext文件系统收尾篇
linux·c语言
牵猫散步的鱼儿7 小时前
重载、重写(覆盖)、重定义区别
c语言
猎头南楼8 小时前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
phltxy8 小时前
C 语言指针:从内存地址到灵活的数据访问
c语言
phltxy8 小时前
C 语言中的数据存储:从类型到二进制位
c语言
旖旎夜光9 小时前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark9 小时前
大规模并行计算中的负载均衡算法研究4
算法