栈表应用c语言

#pragma once
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STpye;

typedef struct Stack {
    STpye* a;
    int top;//zhan ding
    int capacity;
}St;

void StackInit(St* ps);
void StackDestory(St* ps);
void StackPush(St* ps, STpye x);
void StackPop(St* ps);
STpye StackTop(St* ps);
int StackSize(St* ps);
bool StackEmpty(St* ps);

#include"Stack.h"
#include<assert.h>


void StackInit(St* ps)//初始化数据
{
    assert(ps);
    ps->a = NULL;
    ps->top = ps->capacity = 0;
}

void StackDestory(St* ps) {
    assert(ps);
    free(ps->a);
    ps->a = NULL;
    ps->top = ps->capacity = 0;
}
void StackPush(St* ps, STpye x) {
    assert(ps);
    if (ps->top == ps->capacity)
    {
        int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        STpye* tmp = (STpye*)realloc(ps->a, sizeof(STpye) * newcapacity);

        if (tmp == NULL) {
            printf("不成了");//增容增好
            exit(-1);
        }
        ps->a = tmp;
        ps->capacity = newcapacity;
    }

    //ps->a[ps->top++] = x;
    ps->a[ps->top ] = x;
    ps->top++;
}
void StackPop(St* ps) {
    assert(ps);
    assert(ps->top > 0);
    ps->top--;
}
STpye StackTop(St* ps) {
    assert(ps);
    //assert(ps->top >= 0);
    return ps->a[ps->top-1];
}
int StackSize(St* ps) {
    assert(ps);
    return ps->top;
}
bool StackEmpty(St* ps) {
    assert(ps);
    /*if (ps->top == 0) {
        return true;
    }
    else {
        return false;
    }*/
    return ps->top == 0;
}

#include"Stack.h"
void tee() {
    St t;
    StackInit(&t);
    StackPush(&t, 1);
    StackPush(&t, 2);
    

    while (!StackEmpty(&t)) {
        printf("%d ", StackTop(&t));
        StackPop(&t);

    }

    StackDestory(&t);
}

int main() {
    tee();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值