数据结构-顺序栈

#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 100
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int Status;
typedef int SElemType;

typedef struct
{
    SElemType *base;
    SElemType  *top;
    int stacksize;
} SqStack;

Status InitStack(SqStack *S);
Status Push(SqStack *S, SElemType e);
Status Pop(SqStack *S, SElemType *e);
Status StackEmpty(SqStack S);
void conversion ();

int main()
{
    printf("Please input a number to conver:\n");
    conversion();
    return 0;
}
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 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;
    }  // if
    *S->top++ = e;
    return OK;
}  //Push
Status Pop(SqStack *S, SElemType *e)
{
    if(S->top == S->base)return ERROR;
    *e = *--S->top;
    return OK;
} //Pop
Status StackEmpty(SqStack S)
{
    if (S.base == S.top)
        return TRUE;
    return FALSE;
}
void conversion ()
{
    SqStack S;
    int N,e;
    InitStack(&S);
    scanf ("%d",&N);
    while (N)
    {
        Push(&S, N % 8);
        N = N/8;
    }
    while (!StackEmpty(S))
    {
        Pop(&S, &e);
        printf ("%d", e);
    }
}



猜你喜欢

转载自blog.csdn.net/DeathIsLikeTheWind/article/details/53982915