zoukankan      html  css  js  c++  java
  • 栈和栈的应用

    #include <stdio.h>
    #include<stdlib.h>
    #define MAXSIZE 110
    #define ok 1
    #define error 0
    typedef int SElemType;
    typedef int Status;
    
    ///顺序栈类型的定义
    typedef struct
    {
        SElemType data[MAXSIZE];
        int top;
    }SeqStack;
    
    ///顺序栈实现的基本操作
    void InitStack(SeqStack &S)///初始化
    {
        S.top=-1;
    }
    
    ///判断栈是否已满,若满,则返回1,否则返回0
    Status StackFull(SeqStack S)
    {
        return S.top==MAXSIZE-1;
    }
    
    ///判断栈是否为空,若为空,则返回1,否则返回0
    Status StackEmpty(SeqStack S)
    {
        return S.top==-1;
    }
    
    Status StackPush(SeqStack &S, SElemType e)
    {
        if(S.top==MAXSIZE-1) return error;
    
        S.top++;
        S.data[S.top]=e;
        return ok;
    }
    
    Status StackPop(SeqStack &S, SElemType *e)///出栈,将出栈元素赋值给e
    {
        if(StackEmpty(S))
        {
            printf("Stack is Empty.UnderFlow!
    ");
            return error;
        }
    
        *e=S.data[S.top];
        S.top--;
        return ok;
    }
    
    Status GetTop(SeqStack &S, SElemType *e)
    {
        if(StackEmpty(S))
        {
            printf("Stack is Empty.UnderFlow!
    ");
            return error;
        }
    
        *e=S.data[S.top];
        return ok;
    }
    
    ///实现n个数的逆序输出
    int main()
    {
        int n, a, num;
        SeqStack S;
    
        while(~scanf("%d", &n))
        {
            InitStack(S);
    
            for(int i=0; i<n; i++)
            {
                scanf("%d", &num);
                if(StackFull(S))
                {
                    printf("栈已满,入栈失败!
    ");
                    break;
                }
    
                else
                    StackPush(S, num);
            }
    
            while(!StackEmpty(S))
            {
                StackPop(S, &a);
                printf("输出出栈元素:%d
    ", a);
            }
    
        }
        return 0;
    }
    #include <stdio.h>
    #define MAXSIZE 100
    #define ok 1
    #define OVERFLOW -1
    #define true 1
    #define false 0
    #define error 0
    
    typedef char SElemType;
    typedef int Status;
    typedef struct
    {
        SElemType data[MAXSIZE];
        int top;
    } SeqStack;
    
    ///判断栈是否为空,若为空,则返回1,否则返回0
    Status StackEmpty(SeqStack S)
    {
        return S.top==-1;
    }
    
    ///判断栈是否已满,若满,则返回1,否则返回0
    Status StackFull(SeqStack S)
    {
        return S.top==MAXSIZE-1;
    }
    
    Status StackPush(SeqStack &S, SElemType e)
    {
        if(S.top==MAXSIZE-1) return error;
    
        S.top++;
        S.data[S.top]=e;
        return ok;
    }
    
    ///顺序栈实现的基本操作
    void InitStack(SeqStack &S)///初始化
    {
        S.top=-1;
    }
    
    Status StackPop(SeqStack &S)///出栈,将出栈元素赋值给e
    {
        if(StackEmpty(S))
        {
            printf("Stack is Empty.UnderFlow!
    ");
            return error;
        }
        S.top--;
        return ok;
    }
    
    Status GetTop(SeqStack &S, SElemType *e)
    {
        if(StackEmpty(S))
        {
            printf("Stack is Empty.UnderFlow!
    ");
            return error;
        }
    
        *e=S.data[S.top];
        return ok;
    }
    
    int main()
    {
        SElemType ch, e;
        int i, f=0;
        SeqStack S;
        char str[110];
        printf("输入字符串:
    ");
        scanf("%s",str);
        InitStack(S);
        StackPush(S, str[0]);
        for(i=1; str[i]!=''; i++)
        {
            ch = str[i];
    
            if(ch == '(' || ch == '[' || ch=='{')
                StackPush(S, ch);
    
            else
            {
                GetTop(S, &e);
                if( ( e == '(' && ch == ')' ) || ( e == '[' && ch == ']' ) || ( e == '{' && ch == '}' ))
                    StackPop(S);
                else
                {
                    f=1;
                    printf("NO
    ");
                    break;
                }
            }
        }
        if(f==0)
            printf("YES
    ");
    
        getchar();
        return 0;
    }
    

      

  • 相关阅读:
    geoServer的安装
    类设计原则
    零散知识点
    JavaScript setTimeOut()方法的一些疑点自己记录
    贴吧表情雨
    Laravel中tosql()是如何返回sql
    关于new static 与 new self的区别(后续有新发现,会继续更新)
    Lumen、Laravel开发问题记录
    文件实时对比,将数据组装入库(SQLITE)
    PHPSTUDY下升级mysql后无法启动
  • 原文地址:https://www.cnblogs.com/w-y-1/p/5990429.html
Copyright © 2011-2022 走看看