栈和栈的应用

#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!\n");
        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!\n");
        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("栈已满,入栈失败!\n");
                break;
            }

            else
                StackPush(S, num);
        }

        while(!StackEmpty(S))
        {
            StackPop(S, &a);
            printf("输出出栈元素:%d\n", 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!\n");
        return error;
    }
    S.top--;
    return ok;
}

Status GetTop(SeqStack &S, SElemType *e)
{
    if(StackEmpty(S))
    {
        printf("Stack is Empty.UnderFlow!\n");
        return error;
    }

    *e=S.data[S.top];
    return ok;
}

int main()
{
    SElemType ch, e;
    int i, f=0;
    SeqStack S;
    char str[110];
    printf("输入字符串:\n");
    scanf("%s",str);
    InitStack(S);
    StackPush(S, str[0]);
    for(i=1; str[i]!='\0'; 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\n");
                break;
            }
        }
    }
    if(f==0)
        printf("YES\n");

    getchar();
    return 0;
}

  

posted @ 2016-10-23 17:30  爱记录一切美好的微笑  阅读(367)  评论(0编辑  收藏  举报