• 栈和栈的应用


    #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;
    }
    

      

  • 相关阅读:
    prototype.js超强的javascript类库
    MySQL Server Architecture
    Know more about RBA redo block address
    MySQL无处不在
    利用Oracle Enterprise Manager Cloud Control 12c创建DataGuard Standby
    LAMP Stack
    9i中DG remote archive可能导致Primary Database挂起
    Oracle数据库升级与补丁
    Oracle为何会发生归档日志archivelog大小远小于联机重做日志online redo log size的情况?
    Oracle Ksplice如何工作?How does Ksplice work?
  • 原文地址:https://www.cnblogs.com/w-y-1/p/5990429.html
Copyright © 2020-2023  润新知