数据结构:栈的应用(数制转换)

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

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10 

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

int InitStack(SqStack& S)
{
    S.base = (int*)malloc(STACK_INIT_SIZE * sizeof(int));
    if (!S.base) return -1;
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return 0;
}

int Push(SqStack& S, int e)
{
    if (S.top - S.base >= S.stacksize)
    {
        S.base = (int*)realloc(S.base,
            (S.stacksize + STACKINCREMENT) * sizeof(int));
        if (!S.base)
        {
            return -1;
        }
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return 1;
}

int Pop(SqStack& S, int& e)
{
    if (S.top == S.base)
    {
        return 0;
    }
    e = *--S.top;
    return 1;
}

int StackEmpty(SqStack S)
{
    if (S.base == S.top)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}



int main()
{
    SqStack S;
    int N, e;
    InitStack(S);
    printf("请输入一个十进制数:\n");
    scanf("%d", &N);
    while (N)
    {
        Push(S, N % 2);
        N = N / 2;
    }
    printf("对应的二进制数为:\n");
    while (!StackEmpty(S))
    {
        Pop(S, e);
        printf("%d", e);
    }
}

#define STACK_INIT_SIZE 100  //存储空间初始分配量
#define STACKINCREMENT 10  //存储空间分配增量

typedef struct
{
    int* base;  //在栈顶构造之前和销毁之后,base的值为NULL
    int* top;   //栈顶指针
    int stacksize;  //当前已分配的存储空间,以元素为单位
}SqStack;  //命名为SqStack

//栈的初始化

int InitStack(SqStack& S)  //构造一个空栈S
{
    S.base = (int*)malloc(STACK_INIT_SIZE * sizeof(int)); 
    if (!S.base) return -1;  //储存分配失败
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return 0;
}

//插入元素e为新的栈顶元素

int Push(SqStack& S, int e)
{
    if (S.top - S.base >= S.stacksize)  //栈满,追加存储空间
    {
        S.base = (int*)realloc(S.base,
            (S.stacksize + STACKINCREMENT) * sizeof(int));
        if (!S.base)  //存储分配失败
        {
            return -1;
        }
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return 1;
}

//若栈不空,则删除S的栈顶元素,用e返回其值,并返回1,否则返回-1

int Pop(SqStack& S, int& e)
{
    if (S.top == S.base)
    {
        return 0;
    }
    e = *--S.top;
    return 1;
}

//判断顺序栈是否为空

int StackEmpty(SqStack S)
{
    if (S.base == S.top)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{
    SqStack S;
    int N, e;
    InitStack(S);
    printf("请输入一个十进制数:\n");
    scanf("%d", &N);
    while (N)
    {
        Push(S, N % 2);
        N = N / 2;
    }
    printf("对应的二进制数为:\n");
    while (!StackEmpty(S))
    {
        Pop(S, e);
        printf("%d", e);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值