poj——1363 栈的使用

#include <iostream>
using namespace std;

typedef struct Node{
	int info;
	Node * link;
}Node;

typedef struct LinkStack{
	Node * top;
}LinkStack;

int push(LinkStack * stack, int x){
	Node * p = new Node;
	p->info = x;
	p->link = stack->top;
	stack->top = p;
	return 1;
}

int pop(LinkStack * stack){
	if (stack->top == NULL){
		printf("栈为空!\n");
		return -1;
	}
	else {
		Node * p;
		p = stack->top;
		stack->top = stack->top->link;
		int t = p->info;
		return t;
	}
}

int main(){
	int train;
	while(scanf("%d",&train)&&train){
		int trains[1001];
		while(1){
			scanf("%d",&trains[0]);
			if(trains[0]==0){
				printf("\n");
				break;
			}
			else {
				for(int x=1;x<train;x++)
					scanf("%d",&trains[x]);
			}
			LinkStack * stack = new LinkStack;
			stack->top = NULL;	
			int num=0;		
			for(int i=0;i<train;i++){
				push(stack,i+1);
				while(stack->top!=NULL&&stack->top->info==trains[num]){
					pop(stack);
					num++;
				}
			}
			if (num==train)
				cout << "Yes" << endl;
			else
				cout << "No" << endl;
		}
	}	
}

  

posted @ 2017-09-06 19:22  Mr.Struggle  阅读(149)  评论(0编辑  收藏  举报