首页 > 其他分享 >zhanshuzhi

zhanshuzhi

时间:2023-04-17 19:07:22浏览次数:31  
标签:SElemType return SqStack zhanshuzhi top base stacksize

#include < stdio.h > 
#include < stdlib.h >
#include "myconst.h"
typedef unsigned int SElemType;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10 
typedef struct {
    SElemType * base;
    SElemType * top;
    int stacksize;
}
SqStack;
Status InitStack(SqStack & S) {
    S.base = (SElemType * ) malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if (!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}
Status GetTop(SqStack S, SElemType & e) {
    if (S.top == S.base) return ERROR;
    e = *(S.top - 1);
    return OK;
}
Status Push(SqStack & S, SElemType e) {
    if (S.top - S.base >= S.stacksize) {
        S.base = (SElemType * ) realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
        if (!S.base) exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    } * S.top++=e;
    return OK;
}
Status Pop(SqStack & S, SElemType & e) {
    if (S.top == S.base) return ERROR;
    e = *--S.top;
    return OK;
}
Status StackEmpty(SqStack S) {
    return ! (S.top - S.base);
}
void conversion() {
    SqStack S;
    int p = 2;
    SElemType N,
    e;
    InitStack(S);
    printf("\n10jinzhi N=");
    scanf("%u", &N);
    while (N) {
        Push(S, N % p);
        N = N / p;
    }
    printf("\n%djinzhi:", p);
    while (!StackEmpty(S)) {
        Pop(S, e);
        printf("%u", e);
    }
}
void main() {
    conversion();
}

标签:SElemType,return,SqStack,zhanshuzhi,top,base,stacksize
From: https://blog.51cto.com/u_16076050/6195965

相关文章