杨老师数据结构栈笔记

杨老师b站视频如下,作为数据结构初学者收益很大,值得推荐:2 数据结构导论_哔哩哔哩_bilibili

.h文件类型定义

#ifndef MY_SEQSTACK_H
#define MY_SEQSTACK_H
#define TRUE  1
#define FALSE 0
#define OK    1
#define ERROR 0
#define OVERFLOW -2
#define NULLPTR -3
#define STACK_INIT_SIZE 100
#define STACKINC        1.5
typedef char SElemType;
typedef int Status;
typedef struct {
    SElemType* base; //栈底指针
    SElemType* top; //栈顶指针
    int stacksize;  //栈的容量
}SeqStack;

.h文件 函数声明(与函数定义保持一致即可,可跳过)

Status InitStack(SeqStack* pstack);
void DestoryStack(SeqStack* pstack);
void ClearStack(SeqStack* pstack);
Status Push(SeqStack* pstack, SElemType val);
Status GetTop(const SeqStack* pstack, SElemType* pval);
Status Pop(SeqStack* pstack, SElemType* pval);
int StackLength(const SeqStack* pstack);
bool StackEmpty(const SeqStack* pstack);
bool StackFull(const SeqStack* pstack);
bool IncSize(SeqStack* pstack);

.cpp文件 函数定义

引入头文件

#include<stdlib.h>
#include<assert.h>
#include "SeqStack.h"

初始化栈

Status InitStack(SeqStack* pstack) {
    assert(pstack != NULL);
    pstack->base = (SElemType*)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
    if (NULL == pstack->base) { return OVERFLOW; }
    pstack->top = pstack->base;
    pstack->stacksize = STACK_INIT_SIZE;
    return OK;
}

摧毁栈

void DestoryStack(SeqStack* pstack) {
    assert(pstack != NULL);
    free(pstack->base);
    pstack->base = NULL;
    pstack->top = NULL;
    pstack->stacksize = 0 ;
}

清空栈

void ClearStack(SeqStack* pstack) {
    assert(pstack != NULL);
    pstack->top = pstack->base;
}

入栈

Status Push(SeqStack* pstack, SElemType val) {
    assert(pstack != NULL);
    if (StackFull(pstack) && !IncSize(pstack)) { return OVERFLOW; }
    //如果栈满了 并且我也进行增容了且失败了 就返回OVERFLOW
    *pstack->top = val;
    pstack->top += 1;
    return OK;
}

获得栈顶

Status GetTop(const SeqStack* pstack, SElemType* pval) {
    assert(pstack != NULL);
    if (StackEmpty(pstack)) { return ERROR; }
    if (NULL == pval) { return NULLPTR; }
    *pval = *(pstack->top - 1);//这里指针要取值给他
    return OK;    
}

出栈

Status Pop(SeqStack* pstack, SElemType* pval) {
    assert(pstack != NULL);
    if (StackEmpty(pstack)) { return ERROR; }
    if (NULL == pval) { return NULLPTR; }
    pstack->top -= 1;//减一就等于是删除了
    *pval = *pstack->top;
    return OK;
}

获得栈的长度

int StackLength(const SeqStack* pstack) {
    assert(pstack != NULL);
    return pstack->top - pstack->base;
}

判断栈是否空

bool StackEmpty(const SeqStack* pstack) {
    assert(pstack != NULL);
    return pstack->top == pstack->base;
}

判断栈是否满

bool StackFull(const SeqStack* pstack) {
    assert(pstack != NULL);
    return StackLength(pstack) == pstack->stacksize;
}

栈增容

bool IncSize(SeqStack* pstack) {
    assert(pstack != NULL);
    int total = pstack->stacksize * STACKINC;
    SElemType* newdata = (SElemType*)realloc(pstack->base, sizeof(SElemType) * total);
    if (NULL == newdata) { return false; }
    pstack->base = newdata;
    pstack->top = pstack->base + pstack->stacksize;//加上原来的stacksize
    pstack->stacksize = total;
    return true;
}

十进制转八进制

void PrintOct(unsigned int n) {
    SeqStack st;
    InitStack(&st);
    unsigned int e;
    while (n != 0) {
        Push(&st, n % 8);//把余数放进去
        n = n / 8;
    }
    while (!StackEmpty(&st)) {
        Pop(&st, &e);//传e的地址
        printf("%d", e);
    }
    printf("\n");
    DestoryStack(&st);//最后一定要摧毁
}

十进制转2-16进制的字符串

char* my_itoa(unsigned int val, char* buff, int radix) {
    if (NULL == buff)return buff;
    SeqStack st;
    InitStack(&st);
    int i;
    char ch;
    static const char digit[] = { '0','1','2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'10' ,'11' ,'12' ,'13','14','15' };
    while (val != 0) {
        //把转换后的字符串放入栈中
        Push(&st, digit[val % radix]);
        val = val / radix;
    }
    i = 0;
    //把所有数都提出来 放到字符数组中
    while (!StackEmpty(&st)) {
        Pop(&st, &ch);
        buff[i++] = ch;
    }
    //字符串最后一个是\0
    buff[i] = '\0';
    DestoryStack(&st);//最后利用完栈肯定要销毁掉
    return buff;
}

判断括号是否匹配

bool Matching(char* str) {
    assert(str != NULL);
    SeqStack st;
    char ch;
    InitStack(&st);
    bool res = true;
    for (int i = 0; str[i] != '\0'; i++) {
        switch (str[i]) 
        {
        case '{':case'[':case '(':Push(&st, str[i]); break;
        case '}':if (StackEmpty(&st) ||(Pop(&st, &ch), ch != '{')) { res=false; } break;  
        case')':if (StackEmpty(&st) || (Pop(&st, &ch), ch != '(')) { res = false; } break;
        case']':if (StackEmpty(&st) || (Pop(&st, &ch), ch != '[')) { res = false; } break;
        default:break;
        }
    }
    if (!StackEmpty) { res = false; }//如果不空的话返回false
    return res;
}

猜你喜欢

转载自blog.csdn.net/weixin_55109830/article/details/129000266