顺序栈的实现

#include <ctype.h>
#include <malloc.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int32_t Elemtype;

typedef struct
{
    Elemtype *Stack_List, *Stack_listEnd;
    Elemtype* top;
    int maxlengh;
} linear_Stack_t, *linear_Stack_p;
linear_Stack_p CreatStack(int maxlengh)
{
    if (maxlengh < 0)
        return NULL;
    linear_Stack_t* newStack = malloc(sizeof(linear_Stack_t));
    if (!newStack)
        return NULL;
    newStack->Stack_List = maxlengh == 0 ? NULL : malloc(sizeof(Elemtype) * maxlengh);
    if (maxlengh > 0 && !newStack->Stack_List) {
        free(newStack);
        return NULL;
    }
    memset(newStack->Stack_List, 0, sizeof(Elemtype) * maxlengh);
    newStack->Stack_listEnd = newStack->Stack_List ? newStack->Stack_List + maxlengh - 1 : NULL;
    newStack->top = NULL;
    newStack->maxlengh = maxlengh;
    return newStack;
}

typedef uint8_t S_Status;

#define OK 0

#define SNULL 1

S_Status isNULLStack(linear_Stack_p s)
{
    return s == NULL ? SNULL : 0;
}

#define SEMPTY 2

S_Status isEmptyStack(linear_Stack_p s)
{
    return s->top == NULL ? SEMPTY : 0;
}

#define SFULL 3
S_Status isFullStack(linear_Stack_p s)
{
    return s->top == s->Stack_listEnd ? SFULL : 0;
}
S_Status (*StackStatusFuc[3])(linear_Stack_p) = { &isNULLStack, &isEmptyStack, &isFullStack };

void PrintStackStatue(linear_Stack_p s)
{
    S_Status status = 0;
    for (int i = 0; i < 3; i++) {
        status = (*StackStatusFuc[i])(s);
        printf("%d ", status);
        if (status == SNULL)
            break;
    }
    printf("\n");
}

S_Status Push(linear_Stack_p s, const Elemtype date, void CopyFuc(Elemtype*, const Elemtype*))
{
    if (isNULLStack(s))
        return SNULL;
    if (isFullStack(s))
        return SFULL;
    s->top = s->top ? s->top + 1 : s->Stack_List;
    CopyFuc(s->top, &date);
    return 0;
}
S_Status Push_num(linear_Stack_p s, int num, Elemtype* date, void CopyFuc(Elemtype*, const Elemtype*))
{
    S_Status status = 0;
    while (!(status = Push(s, *date, CopyFuc)) && num--) {
        ++date;
    }
    return status;
}

S_Status Pop(linear_Stack_p s, Elemtype* date, void CopyFuc(Elemtype*, const Elemtype*))
{
    if (isNULLStack(s))
        return SNULL;
    if (isEmptyStack(s))
        return SEMPTY;
    CopyFuc(date, s->top);
    s->top = s->top == s->Stack_List ? NULL : s->top - 1;
    return 0;
}

#define NO_LARGENUM_TO_POP SEMPTY

S_Status Pop_num(linear_Stack_p s, int num, Elemtype* date, void CopyFuc(Elemtype*, const Elemtype*))
{
    S_Status status = 0;
    while (!(status = Pop(s, date, CopyFuc)) && num--) {
        ++date;
    }
    return status;
}

S_Status Pop_All(linear_Stack_p s, Elemtype* date, void CopyFuc(Elemtype*, const Elemtype*))
{
    S_Status status = 0;
    while (!(status = Pop(s, date, CopyFuc))) {
        ++date;
    }
    return status == SEMPTY ? 0 : status;
}

void ElemtypeCopyFuc(Elemtype* dst, const Elemtype* src)
{
    *dst = *src;
}



int main(void)
{
    Elemtype pushdate[10] = {1,2,3,4,5,6,7,8,9,0};
    Elemtype popdate[10];
    linear_Stack_p s1 = CreatStack(5);
    linear_Stack_p s2 = CreatStack(3);
    Push_num(s1,5,pushdate,ElemtypeCopyFuc);
    Push_num(s2,3,pushdate+5,ElemtypeCopyFuc);
    printf("\n");
    Pop_All(s1,popdate,ElemtypeCopyFuc);
    Pop_All(s2,popdate + 5,ElemtypeCopyFuc);
    printf("\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值