• C语言顺序栈实现


    /*数序栈*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define SElemType char
    #define STACK_INIT_SIZE   100
    #define STACK_INCREMENT 10
    #define OK 1
    #define ERROR -1
    #define Status int
    #define OVER -1
    /*栈空 top == base
    **栈满 top-base = stacksize
    **
    */
    typedef  struct
    {
        SElemType *base;
        SElemType *top;
        int stacksize;
        int length;
    }SqStack;
    /*数序栈结构*/
    
    /*栈的初始化*/
    Status  initSqStack(SqStack *s)
    {
        s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
        if(! s->base) return OVER;
        s->top = s->base;
        s->stacksize = STACK_INIT_SIZE;
        s->length = 0;
        return OK;
    }
    Status pushSqStack(SqStack *s,const SElemType item)
    {
        /* stack full*/
        if(s->top - s->base == STACK_INIT_SIZE)
        {
            s->base = (SElemType*)realloc(s->base,(STACK_INCREMENT+STACK_INIT_SIZE)*sizeof(SElemType));
            if(!s->base) return OVER;
            s->top = s->base+ s->stacksize;
            s->stacksize += STACK_INCREMENT;
        }
        *s->top++ = item;
    
        return OK;
    }
    
    Status popSqStack(SqStack *s,SElemType *item)
    {
        if(s->top == s->base) return ERROR;
        *item = *--(s->top);
        return OK;
    }
    /*获取栈长度*/
    int lengthSqStack(SqStack *s)
    {
        return s->top-s->base;
    }
    
    int main(void)
    {
        SElemType data;
        SqStack s;
        int len,i,sum =0;
        printf("Please input a binary digit
    ");
        initSqStack(&s);
        scanf("%c",&data);
        while(data !='#')
        {
            pushSqStack(&s,data);
            scanf("%c",&data);
        }
        getchar();
        len = lengthSqStack(&s);
        for(i = 0;i<len;i++)
        {
            popSqStack(&s,&data);
            printf("%c",data);
            sum += (data-48)*pow(2,i);
        }
        printf("Decimal is :%d.
    ",sum);
        getchar();
    
        return 0;
    }
  • 相关阅读:
    dedecms列表页实现文档按权重weight排序
    DEDEcms5.7各个文件的作用与安全防御设置
    我的SEO笔记
    分析网站日志
    网页上链接的添加规范与标准
    解决引用bootstrap样式时遇到的冲突问题
    js滚动到指定位置导航栏固定顶部
    使用rem自适应屏幕
    SQL 操作结果集 -并集、差集、交集、结果集排序
    简洁的左右滑动式轮播图
  • 原文地址:https://www.cnblogs.com/plxx/p/3489187.html
Copyright © 2020-2023  润新知