栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct node
{
    int * base;
    int * top;
    int stacksize;
}Sqstack;
void InitStack(Sqstack *S)
{
    S->base = (int *)malloc(sizeof(int)*100);
    if(!S->base)
        exit(0);
    S->top=S->base;
    S->stacksize=100;
}
void Push(Sqstack *S,int elem)
{

    *(S->top++)=elem;

}
void Pop(Sqstack *S,int * elem)
{
    S->top--;
    *elem=*(S->top);
}

int main()
{
    Sqstack S;
    int data,i,a,b;
    InitStack(&S);
    printf("Input a number:");
    scanf("%d",&data);
    while(!a==0)
    {
        a=data/2;
        b=data-a*2;
        data=a;
        Push(&S,b);


    }
    printf("transformed to 2 jinzhi :\n");
    while(S.top!=S.base)
    {
        Pop(&S,&i);
        printf("%d",i);
    }
    printf("\n");
}

猜你喜欢

转载自blog.csdn.net/dididisailor/article/details/50336973