码迷,mamicode.com
首页 > 其他好文 > 详细

栈之出栈序列合法性

时间:2015-04-18 23:30:28      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<string.h>
#define MaxSize 100
typedef int DataType;
typedef struct
{
    DataType stack[MaxSize];
    int top;
}SeqStack;
void StackInitiate(SeqStack *s)//初始化 
{
    s->top=0;
}
int StackNotEmpty(SeqStack s)//非空否 
{
    if(s.top<= 0) return 0;
    else return 1;
}
int StackPush(SeqStack *s,DataType x)//入栈 
{
    if(s->top>=MaxSize)
    {
        return 0;
    }
    else
    {
        s->stack[s->top]=x;
        s->top++;
        return 1;
    }
} 
int StackPop(SeqStack *s,DataType *d)//出栈 
{
    if(s->top<=0)
    {
        return 0;
    }
    else
    {
        s->top--;
        *d=s->stack[s->top];
        return 1;
    }
}
int StackTop(SeqStack s,DataType *d)//取栈顶数据 
{
    if(s.top<=0)
    {
        return 0;
    }
    else
    {
        *d=s.stack[s.top-1];
        return 1;
    }
} 


 main()
{
    SeqStack l;
    int i,n=0,j,k,ru,chu[100],t,first=1;
    StackInitiate(&l);
    while(scanf("%d",&chu[n])!=EOF)
    {
        n++;
    }
    i=0,ru=1;
    while(i<n)
    {
        if(!StackNotEmpty(l))
        {
            StackPush(&l,ru);
            ru++;
        }
        else
        {
            StackTop(l,&t);
            if(t==chu[i])
            {
                StackPop(&l,&t);
                i++;
            }
            else if(t<chu[i])
            {
                StackPush(&l,ru);
                ru++;
            }
            else
            {
                first=0;
                break;
            }
            
        }
    }
    if(first)
    printf("1");
    else
    printf("0");
} 

 

栈之出栈序列合法性

标签:

原文地址:http://www.cnblogs.com/yc721274287/p/4438361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!