SqStack ary calculation

SqStack ary calculation based on

#include<malloc.h> 
#include<stdio.h> 
#include<stdlib.h> 
typedef int Status;
typedef int SElemType;
#define STACK_INIT_SIZE 100 
#define STACKINCREMENT 20
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef struct SqStack
{
    SElemType *base;
    SElemType *top;
    int stacksize;
} SqStack; // 顺序栈


Status InitStack(SqStack & S) 
{ // Constructs an empty stack S 
    IF ((S!. Base = (SElemType *) the malloc (* STACK_INIT_SIZE the sizeof ) (SElemType))) 
        Exit (the OVERFLOW); // memory allocation failure 
    S.top = S. Base ; 
    S.stacksize = STACK_INIT_SIZE;
     return the OK; 
} 
int getTop (SqStack S) 
{ // If the stack is not empty, the top element is returned by S E 
    IF (S.top> S. Base )
         return * (S. Top - . 1 ); 
} 

the Status the Push (SqStack &S, SElemType e) 
{ // insert a new element e of the top element 

    IF (S.top - S. Base > = S.stacksize) // stack is full, the additional storage space 
    { 

        S. Base = (SElemType *) realloc (S. Base , (+ S.stacksize STACKINCREMENT) * the sizeof (SElemType));
         IF (S!. Base ) 
            Exit (the OVERFLOW); // memory allocation failure 
        S.top S. = Base + S.stacksize; 
        S. STACKSIZE + = STACKINCREMENT; 
    }
     * S.top = E; 
    S.top ++;
     Return the OK; 
} 

the Status Pop (SqStack & S, SElemType & E) 
{ // If the stack is not empty, then remove the top element of S, which returns the value E, and returns the OK; otherwise ERROR 
    IF (= S.top S. = Base )
         return ERROR;
     // E * = - S.top; // E * = S.top; 
    S.top-- ; 
    E = * S.top;
     return E; 
} 

the Status StackTraverse (S SqStack , the Status ( * visit) (SElemType)) 
{ // sequentially visit the calling function for each element of the stack from the stack bottom to top of the stack ().
  // Once visit () fails, then the operation fails 
    the while (S.top> S. Base) 
        Visit ( * S. Base ++ ); 
    the printf ( " \ n- " );
     return the OK; 
} 
int main () 
{ 
    SqStack S; 
    int X, Y, E, num1; 
    ; InitStack (S) 
    the printf ( " input decimal number: \ n- " ); 
    Scanf ( " % D " , & X); 
    the printf ( " enter a decimal number: \ n- " ); 
    Scanf ( " % D " , & num1);
     the while (X) 
    { 
        the Push ( S, x% num1);
        x = x / num1;
    }
    while (S.base != S.top)
    {
        y = Pop(S, e);
        printf("%d", y);
    }
    printf("\n======================\n");

    system("pause");
    return 0;
}

Reference: https://blog.csdn.net/sunshunli/article/details/78461172

Guess you like

Origin www.cnblogs.com/herd/p/11761629.html