数据结构与算法:栈-动态数组实现(c语言实现,带详细注释)

//Stack.c
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef int STDataType;

typedef struct Stack
{
    STDataType* pArr;
    int capacity;
    int top;
    
}ST, *pST;

void Stack_Init(pST pstack);

void Stack_Push(pST pstack, STDataType pvalue);
void Stack_Pop(pST pstack);

int Stack_Size(pST pstack);
bool Stack_IsEmpty(pST pstack);

STDataType Stack_Top(pST pstack);
void Stack_Destory(pST pstack);

void Stack_Print(ST stack);
//Stack.c
#include "stack.h"  // 包含自定义的堆栈头文件

// 初始化堆栈
void Stack_Init(pST pstack)
{
    assert(pstack); // 断言pstack不为NULL
    pstack->pArr = (STDataType*)malloc(sizeof(STDataType)*4); // 分配4个元素大小的堆内存作为堆栈的数组
    if(pstack->pArr == NULL) // 检查内存分配是否成功
    {
        printf("malloc failed!"); // 分配失败,输出错误信息
        exit(-1); // 退出程序,返回-1表示异常终止
    }
    pstack->capacity = 4; // 设置堆栈的容量为4
    pstack->top = 0; // 初始化堆栈的栈顶指针为0
}

// 入栈操作
void Stack_Push(pST pstack, STDataType value)
{
    assert(pstack); // 断言pstack不为NULL

    if(pstack->top == pstack->capacity) // 如果栈满
    {
        // 重新分配堆内存,将堆栈的容量扩展为原来的2倍
        STDataType* enlarged = (STDataType*)realloc(pstack->pArr, sizeof(STDataType)*2*pstack->capacity);
        pstack->pArr = enlarged;
        pstack->capacity *= 2; // 更新堆栈的容量为扩展后的值
    }

    pstack->pArr[pstack->top] = value; // 将元素值value入栈
    pstack->top++; // 更新栈顶指针,指向新的栈顶元素位置
}

// 出栈操作
void Stack_Pop(pST pstack)
{
    assert(pstack); // 断言pstack不为NULL

    if(Stack_IsEmpty(pstack)) // 检查堆栈是否为空
    {
        printf("Stack is empty"); // 若为空,输出提示信息
    }
    else
        pstack->top--; // 若不为空,栈顶指针减1,表示出栈操作
}

// 获取堆栈元素个数
int Stack_Size(pST pstack)
{
    return pstack->top; // 返回堆栈的栈顶指针值,即堆栈的元素个数
}

// 检查堆栈是否为空
bool Stack_IsEmpty(pST pstack)
{
    assert(pstack); // 断言pstack不为NULL

    if(pstack->top == 0) // 栈顶指针为0,表示堆栈为空
    {
        return true; // 返回true表示堆栈为空
    }
    else
        return false; // 返回false表示堆栈不为空
}

// 获取堆栈顶部元素的值,但不出栈
STDataType Stack_Top(pST pstack)
{
    assert(pstack); // 断言pstack不为NULL
    assert(pstack->top > 0); // 断言堆栈不为空

    return pstack->pArr[pstack->top - 1]; // 返回堆栈顶部元素的值
}

// 销毁堆栈,释放堆内存
void Stack_Destory(pST pstack)
{
    assert(pstack); // 断言pstack不为NULL

    free(pstack->pArr); // 释放堆栈数组的内存
    pstack->pArr = NULL; // 将堆栈数组指针置为NULL,避免空指针
    pstack->capacity = 0; // 将堆栈容量置为0
    pstack->top = 0; // 将堆栈栈顶指针置为0
}

// 主函数
int main()
{
    ST stack; // 定义堆栈变量
    Stack_Init(&stack); // 初始化堆栈

    // 将元素依次入栈
    Stack_Push(&stack, 1);
    Stack_Push(&stack, 2);
    Stack_Push(&stack, 3);
    Stack_Push(&stack, 4);
    Stack_Push(&stack, 5);
    Stack_Push(&stack, 6);
    Stack_Push(&stack, 7);
    
    // 依次出栈并打印出栈的元素值
    while (!Stack_IsEmpty(&stack))
    {
        printf("%d ", Stack_Top(&stack));
        Stack_Pop(&stack);
    }
    printf("\n");

    Stack_Destory(&stack); // 销毁堆栈,释放内存
    return 0; // 返回0表示正常结束程序
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值