中国大学MOOC-陈越、何钦铭-数据结构-2017秋 02-线性结构4 Pop Sequence

中国大学MOOC-陈越、何钦铭-数据结构-2017秋 02-线性结构4 Pop Sequence

题目位置

分析:
因pop 时,需要检查栈顶 是否和 要求匹配,
No:
1、如不匹配 则push 元素,当元素都push完仍不匹配,则 输出no
Yes:
当元素匹配完毕后 输出yes
因自己看,,所以参数命名很别扭,表示并不想改了…

#include <stdio.h>
#include <stdlib.h>
struct Stack{
    int size;
    int maxNum;
    int checkNum;
}stack;
void checkFun(struct Stack stack,int numStack[],int list[][stack.maxNum]);
int main(){
    scanf("%d %d %d",&stack.size,&stack.maxNum,&stack.checkNum);
    int numStack[stack.size];
    int list[stack.checkNum][stack.maxNum];
    int i,j;
    for(i=0;i<stack.checkNum;i++){
        for(j=0;j<stack.maxNum;j++)
        {
            scanf("%d",&list[i][j]);
        }
    }
    checkFun(stack,numStack,list);
}
void checkFun(struct Stack stack,int numStack[],int list[][stack.maxNum]){
        int i;
//        for(i=0;i<stack.checkNum;i++){
//        for(j=0;j<stack.maxNum;j++)
//        {
//            printf("%d",list[i][j]);
//        }
//    }
        int topStack=-1;
        int checkNum=0;
        int popNum=1;
        for(i=0;i<stack.checkNum;i++)
        {

                while(1){
//                printf("%d %d %d\n",topStack,popNum,checkNum);
                if(checkNum==stack.maxNum)
                {   printf("YES\n"),topStack=-1,popNum=1,checkNum=0;break;}
                //成功
                if(topStack==-1)topStack++,numStack[topStack] = popNum,popNum++;
                if(numStack[topStack]==list[i][checkNum])checkNum++,topStack--;
                //相等的情况,pop出元素,检查下一个元素。
                else{
                    if(topStack==stack.size-1){printf("NO\n"),topStack=-1,popNum=1,checkNum=0;break;}
                    //stack满,且stack top 与 check num 不相等。
                    if(popNum==stack.maxNum+1){printf("NO\n"),topStack=-1,popNum=1,checkNum=0;break;}
                    //元素push尽,失败
                    topStack++,numStack[topStack] = popNum,popNum++;
                }
                //不相等的情况,push元素,继续检查
                }


        }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值