• 顺序栈(进制转换 + 括号匹配 + 判断栈表 + 最长括号匹配长度)


    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW   -2
    
    #define STACK_INIT_SIZE 100
    #define STACKLINCREMENT 10
    typedef int Status;
    typedef int SElemType;
    
    typedef struct{
        SElemType *base;
        SElemType *top;
        int stacksize;
    }SqStack;
    
    Status InitStack(SqStack &S){
        //构造一个空栈S
        S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
        if(!S.base) exit(OVERFLOW);
        S.top = S.base;
        S.stacksize = STACK_INIT_SIZE;
        return OK;
    }
    
    Status Empty(SqStack S){
        //判断栈是否为空
        if( S.base == S.top)
        return OK;
        return FALSE;
    }
    
    Status GetTop(SqStack S){
        //若栈不空,则用e返回S的栈顶元素,并返回OK,否则返回ERROR
        int e;
        if(S.top == S.base) return ERROR;
        e = *(S.top - 1);
        return e;
    }
    
    Status Push(SqStack &S,SElemType e){
        //插入元素e为新的栈顶元素
        if(S.top - S.base >= S.stacksize){
            //栈满
            S.base = (SElemType *) realloc(S.base,(S.stacksize + STACKLINCREMENT) * sizeof(SElemType));
    
            if(!S.base) exit(OVERFLOW);
            S.top = S.base + S.stacksize;
            S.stacksize += STACKLINCREMENT;
        }
    
        *S.top++ = e;
        return OK;
    }
    
    int StackLength(SqStack S){
        //返回栈的长度
        return (S.top - S.base);
    
    }
    Status Pop(SqStack &S,SElemType &e){
        //若栈不空,则删除S的栈顶元素,用e返回其值,并返回KO,否则返回ERROR
        if(S.top == S.base) return ERROR;
    
        e = *--S.top;
        return OK;
    }
    
    void StackTraverse(SqStack S){
        while(S.top > S.base){
            printf("%d
    ",*S.base++);
        }
        printf("
    ");
    }
    
    
    
    
    //进制转换
    int main()
    {
        int m,k,e;
        while(cin>>m>>k)
        {
            if(m<0) {m=-m;cout<<'-';}
            SqStack S;
            InitStack(S);
            while(m!=0)
            {
                Push(S,m%k);
                m=m/k;
            }
            while(!Empty(S))
            {
    
                if(GetTop(S)==10) cout<<'A';
                else if(GetTop(S)==11) cout<<'B';
                else if(GetTop(S)==12) cout<<'C';
                else if(GetTop(S)==13) cout<<'D';
                else if(GetTop(S)==14) cout<<'E';
                else if(GetTop(S)==15) cout<<'F';
    
                else cout<<GetTop(S);
                Pop(S,e);
            }
            cout<<endl;
        }
        return 0;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    //括号匹配
    int main()
    {
        int e;
        char c[100];
        while(cin.getline(c,100))
        {
            //stack<char>st;
            SqStack S;
            InitStack(S);
            int d = 1;
            int len =strlen(c);
            for(int i = 0; i < len; i++)
            {
                if(c[i]=='('||c[i]=='{'||c[i]=='[')
                                Push(S,c[i]);
                else if(c[i]==')'&&!Empty(S)&&GetTop(S)=='(')
                                Pop(S,e);
                else if(c[i]==')'&&!Empty(S)&&GetTop(S)!='(')
                                d=0;
                else if(c[i]==')'&&Empty(S))
                                d=0;
                else if(c[i]==']'&&!Empty(S)&&GetTop(S)=='[')
                                Pop(S,e);
                else if(c[i]==']'&&!Empty(S)&&GetTop(S)!='[')
                                d=0;
                else if(c[i]==']'&&Empty(S))
                                d=0;
                else if(c[i]=='}'&&!Empty(S)&&GetTop(S)=='{')
                                Pop(S,e);
                else if(c[i]=='}'&&!Empty(S)&&GetTop(S)!='{')
                                d=0;
                else if(c[i]=='}'&&Empty(S))
                                d=0;
    
                }
            if(!Empty(S)||d == 0) cout<<"no"<<endl;
            else cout<<"yes"<<endl;
        }
        return 0;
    }
    
    
    
    //判断栈表
    int main()
    {
        int n,e,target[100];
        while(scanf("%d", &n) == 1)
        {
            SqStack S;
            InitStack(S);
            int A = 1, B = 1;
            for(int i = 1; i <= n; i++)
            {
                scanf("%d",&target[i]);
            }
            int ok = 1;
            while(B <= n)
            {
                if(A == target[B])
                {
                    A++, B++;
                }
                else if(!Empty(S) && GetTop(S) == target[B])
                {
                    Pop(S,e);
                    B++;
                }
                else if(A <= n) Push(S,A++);
                else
                {
                    ok = 0;
                    break;
                }
            }
            printf("%s
    ",ok ? "Yes" : "No");
        }
        return 0;
    }
    
    
    
    
    
    
    
    
    //最长括号匹配长度
    int main()
    {
        char st[1000000];
        int t,e;
        while(cin>>t)
        {
            while(t--)
            {
                cin>>st;
                int len=strlen(st);
                SqStack S;
                InitStack(S);
                for(int i = 0;i < len;i++)
                {
                    if(st[i]=='(') Push(S,st[i]);
                    else if(st[i]==')'&& !Empty(S)&&GetTop(S)=='(') Pop(S,e);
                    else if(st[i]==')') Push(S,st[i]);
                }
                cout<<len - StackLength(S)<<endl;
            }
        }
        return 0;
    }

    链栈: 

    typedef SElemType ElemType;
    typedef LinkList LinkStack;
    
    #define InitStack InitList
    #define DestroyStack DestroyList
    #define ClearStack ClearList
    #define StackEmpty ListEmpty
    #define StackLength ListLength
    
    Status GetTop(LinkStack S,SElemType e)
    {
        return GetElem(S,1,e);
    }
    
    Status Push(LinkStack &S,SElemType e)
    {
        return ListInsert(S,1,e);
    }
    
    Status Pop(LinkStack &S,SElemType &e)
    {
        return ListDelete(S,1,e);
    }
    
    void StackTraverse(LinkStack S)
    {
        LinkStack temp,p = S;
        InitStack(temp);
        while(p)
        {
            Push(temp,p->data);
            p = p->next;
        }
        ListTraverse(temp);
    }
    
    int main()
    {
        return 0;
    }
  • 相关阅读:
    c语言产生随机数的方法
    二叉树递归建立
    二叉树的非递归建立
    建立链表并逆序打印该链表
    C语言中最常用标准库函数
    九个uname命令获取Linux系统详情的实例
    Ubuntu12.04安装Chrome浏览器,并添加到左侧的启动栏
    编程判断大端字节序和小端字节序
    将十进制转换成二进制输出,递归写法
    Lucene介绍及简单入门案例(集成ik分词器)
  • 原文地址:https://www.cnblogs.com/chenyang920/p/5002497.html
Copyright © 2020-2023  润新知