算法:堆栈(转换八进制)

来源:互联网 发布:java se8.0 编辑:程序博客网 时间:2024/06/06 18:22
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>
#define initstack 100
#define Error 0
#define OK 1
#define FALSE-1
typedef int Status;
typedef int SElemType;
typedef struct {
    SElemType *base;
    SElemType *top;
    int stacksize;
}Sqstack;

Status InitStack(Sqstack &S){
    S.base = (SElemType *)malloc(initstack *sizeof(SElemType));
    if(!S.base) return Error;
    S.top=S.base;
    S.stacksize=initstack;
    return OK;
}
Status Pop(Sqstack &S,SElemType &e){
        if(S.base == S.top)
             return Error;
        e = *(--S.top);
    
    
        return OK;
}
Status Push(Sqstack &S,SElemType e){
            *S.top++ = e;
            return OK;
}
Status StackEmpty(Sqstack &S){
    if(S.top == S.base){
        return OK;
    }
    return Error;
}
int main(int argc, char** argv) {
    SElemType e=0;
    Sqstack S;
    InitStack(S);
    SElemType N;
    scanf("%d",&N);
    while(N){
        Push(S,N%8);
        N/=8;
    }
    while(!StackEmpty(S)){
        Pop(S,e);
        printf("%d",e);
    }
    return 0;
}
原创粉丝点击