数据结构 - 栈

214 阅读2分钟

在阅读本文前, 请先阅读以下一篇文章.

数据结构 - 顺序表 - 掘金 (juejin.cn)

一. 栈的定义

栈是一种特殊的线性表, 其只允许在固定的一端进行插入和删除操作, 进行数据插入和删除操作的一端称为栈顶, 另一端称为栈底. 栈中的数据元素遵守后进先出 LIFO (Last In First Out) 的原则.

image.png

二. 栈各种接口的实现

此处的栈采用顺序表的结构实现, 因为顺序表在尾上插入数据的代价相对链表较小.

image.png

typedef int STDataType;

typedef struct Stack
{
    STDataType* a;    // 指向栈底的指针
    int top;    // 栈中有效数据个数
    int capacity;    // 栈的容量大小
}ST;

1. 初始化

创建好结构体后, 对其进行初始化.

void STInit(ST* ps)
{
    assert(ps);

    ps->a = (STDataType*)malloc(sizeof(STDataType) * 4); 
    if (ps->a == NULL) {
        perror("malloc fail");
        return;
    }

    ps->capacity = 4;
    ps->top = 0;
}

2. 销毁

当不再使用栈时, 将动态开辟的数组销毁.

void STDestroy(ST* ps)
{
    assert(ps);

    free(ps->a);
    ps->a = NULL;
    ps->top = 0;
    ps->capacity = 0;
}

3. 入栈

入栈, 即将一个数据元素放入栈中.

image.png

void STPush(ST* ps, STDataType x)
{
    assert(ps);

    if (ps->top == ps->capacity) {
        STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity * 2);
        if (tmp == NULL) {
            perror("realloc fail");
            return;
        }

        ps->a = tmp;
        ps->capacity *= 2;
}

    ps->a[ps->top] = x;
    ps->top++;
}

4. 出栈

出栈, 即将栈顶的数据元素从栈中弹出.

image.png

void STPop(ST* ps)
{
    assert(ps);
    assert(!STEmpty(ps));

    ps->top--;
}

5. 获取栈中有效元素个数

获取栈中有效元素个数.

void STSize(ST* ps)
{
    assert(ps);

    return ps->top;
}

6. 检测栈是否为空

检测栈是否为空.

bool STEmpty(ST* ps)
{
    assert(ps);

    return ps->top == 0;
}

7. 返回栈顶元素

返回栈顶元素.

STDataType STTop(ST* ps)
{
    assert(ps);
    assert(!STEmpty(ps));

    return ps->a[ps->top - 1];
}

三. 栈的源码

Stack.h

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

typedef int STDataType;

typedef struct Stack
{
    STDataType* a;    // 指向栈底的指针
    int top;    // 栈中有效数据个数
    int capacity;    // 栈的容量大小
}ST;

// 初始化
void STInit(ST* ps);

// 销毁
void STDestroy(ST* ps);

// 入栈
void STPush(ST* ps, STDataType x);

// 出栈
void STPop(ST* ps);

// 获取栈中有效元素个数
void STSize(ST* ps);

// 检测栈是否为空
bool STEmpty(ST* ps);

// 返回栈顶元素
STDataType STTop(ST* ps);

Stack.c

#include "Stack.h"

void STInit(ST* ps)
{
    assert(ps);

    ps->a = (STDataType*)malloc(sizeof(STDataType) * 4); 
    if (ps->a == NULL) {
        perror("malloc fail");
        return;
    }

    ps->capacity = 4;
    ps->top = 0;
}

void STDestroy(ST* ps)
{
    assert(ps);

    free(ps->a);
    ps->a = NULL;
    ps->top = 0;
    ps->capacity = 0;
}

void STPush(ST* ps, STDataType x)
{
    assert(ps);

    if (ps->top == ps->capacity) {
        STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity * 2);
        if (tmp == NULL) {
            perror("realloc fail");
            return;
        }

        ps->a = tmp;
        ps->capacity *= 2;
}

    ps->a[ps->top] = x;
    ps->top++;
}

void STPop(ST* ps)
{
    assert(ps);
    assert(!STEmpty(ps));

    ps->top--;
}

void STSize(ST* ps)
{
    assert(ps);

    return ps->top;
}

bool STEmpty(ST* ps)
{
    assert(ps);

    return ps->top == 0;
}

STDataType STTop(ST* ps)
{
    assert(ps);
    assert(!STEmpty(ps));

    return ps->a[ps->top - 1];
}