栈的简单操作,没什么可说的。记得出栈要彻底!
#include<stdio.h> #include<string.h> int n; int a[1005],stack[1005],b[1005]; int solve(int sta[],int tb[],int ta[]) { int i,j,k; int top; sta[0]=1005; i=0;j=0;k=0; sta[1]=ta[i]; top=1; while(i<n&&j<n) { if(sta[top]==tb[j]&&j<n) { while(sta[top]==tb[j]&&j<n&&top>0) { j++;top--; } } else { top++;i++; sta[top]=ta[i]; } } if(top>0&&j<n) { if(sta[top]==tb[j]&&j<n) { while(sta[top]==tb[j]&&j<n&&top>0) { j++;top--; } } } if(top==0) return 1; return 0; } int main() { int i; while(scanf("%d",&n)!=EOF) { if(n==0) break; while(scanf("%d",&a[0])!=EOF) { if(a[0]==0) break; for(i=1;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) b[i]=i+1; if(solve(stack,a,b)) printf("Yes\n"); else printf("No\n"); } printf("\n"); } }
栈操作解析
本文介绍了一个简单的栈操作实现,包括如何通过栈来判断一个序列是否可以通过入栈和出栈操作得到另一个序列。文章提供了完整的C语言代码示例,并通过具体算法说明了如何进行栈的管理和操作。
1786

被折叠的 条评论
为什么被折叠?



