zoukankan      html  css  js  c++  java
  • 第三章:2.栈和队列 -- 栈的应用举例

    前言:

       本节为栈的应用举例,只包括代码实现部分

    目录:

      2.栈的应用举例

        进制转换:

        括号匹配:

      

    正文:

      进制转换实现代码:

      注意:此函数要和上一节,栈的实现代码放在一起

    //进制转换
    void conversion(){
        SqStack S;
        InitStack(S);
        int num;
        printf("%s","请输入一个十进制数:");
        scanf("%d",&num);
        while(num){
            Push(S,num%8);
            num=num/8;
        }
        printf("%s","转换8进制后为:");
        while(S.base!=S.top){
            SElemType e;
            Pop(S,e);
            printf("%d",e);
        }
    }

      

      括号匹配代码实现:

        匹配代码在 CharMatch(char *pc) 函数内实现,其他部分都是栈的基本操作。

    #include<stdio.h>
    #include<stdlib.h>
    #include <string.h>
    
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW -2
    
    #define STACK_INIT_SIZE 100
    #define STACKINCREMENT 10
    //Status是函数的类型,其值是函数结果状态码
    typedef int Status;
    //typedef int SElemType;
    typedef char SElemType;
    
    typedef struct {
        SElemType * base;
        SElemType * top;
        int stacksize;
    }SqStack;
    
    //构造空栈
    Status InitStack(SqStack &S){
        S.base=(SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
        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;
        }
        *S.top++=e;
        return OK;
    }
    
    //删除元素(出栈)
    Status Pop(SqStack &S,SElemType &e){
        if(S.top!=S.base){
            e=*(--S.top);
        }else{
            return ERROR;
        }
        return OK;
    }
    
    
    
    void printAllValues(SqStack &S){
        SElemType * q=S.top;
        printf("top:%p
    ",q);
        while(q!=S.base){
            printf("地址:%p,",q-1);
            //printf("值:%d
    ",*(q-1));
            printf("值:%c
    ",*(q-1));
            q--;
        }
        printf("base:%p
    ",S.base);
    }
    
    //gettop获取栈顶元素
    SElemType GetTop(SqStack &S){
        if(S.base==S.top){
            return 0;
        }
        return *(S.top-1);
    }
    
    //进制转换
    void conversion(){
        SqStack S;
        InitStack(S);
        int num;
        printf("%s","请输入一个十进制数:");
        //scanf("%d",&num);
        scanf("%c",&num);
        while(num){
            Push(S,num%8);
            num=num/8;
        }
        printf("%s","转换8进制后为:");
        while(S.base!=S.top){
            SElemType e;
            Pop(S,e);
            //printf("%d",e);
            printf("%c",e);
        }
    }
    
    
    //括号匹配 包括:{} [] ()
    void CharMatch(char *pc){
        SqStack S;
        InitStack(S);
    
        for(int i=0;pc[i]!=0;i++){
            char d=pc[i];
            char top=GetTop(S);
            if((top=='['&&d==']')||(top=='{'&&d=='}')||(top=='('&&d==')')){
                SElemType e;
                Pop(S,e);
            }else{
                Push(S,d);
            }    
        }
    
        if(S.base!=S.top){
            printf("%s
    ","括号匹配错误");
        }else{
            printf("%s
    ","括号匹配正确");
        }
    }
    
    
    void main(){    
        
        char *c="[{()}(])";
    
        CharMatch(c);
    }
  • 相关阅读:
    线性代数思维导图——3.向量
    微分中值定理的基础题型总结
    构造函数
    Python课程笔记(七)
    0241. Different Ways to Add Parentheses (M)
    0014. Longest Common Prefix (E)
    0013. Roman to Integer (E)
    0011. Container With Most Water (M)
    0010. Regular Expression Matching (H)
    0012. Integer to Roman (M)
  • 原文地址:https://www.cnblogs.com/ahguSH/p/6188449.html
Copyright © 2011-2022 走看看