zoukankan      html  css  js  c++  java
  • 数据结构-顺序栈

    #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:
    ");
        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);
        }
    }
    
    
    

  • 相关阅读:
    HTML5学习小结
    HTML和CSS的复习总结
    LOL UVALive
    E
    D
    C
    B
    D
    J
    css
  • 原文地址:https://www.cnblogs.com/chinashenkai/p/9451397.html
Copyright © 2011-2022 走看看