MOOC数据结构PTA-02-线性结构4 Pop Sequence (25 分)
题目
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.
Output Specification:
For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.
Sample Input:
5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2
Sample Output:
YES
NO
NO
YES
NO
分析
题目要求给出一个深度为M的栈,按12345顺序压入,弹出序列有N个元素,判断K个序列是否顺序正确。
思路
基本思路:先建立好栈的各种操作(初始化,push,pop,isfull,isempty等),然后判断input数字与栈顶元素的大小关系,如果相等则pop,如果大于栈顶且大于迄今为止push的数,则push到此数。最后判断链表是否为空,如空则是按照顺序pop的,输出YES
程序
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#define MAXIMUM 1000
typedef struct Node{
int data;
struct Node* next;
} Node,*Stack;
Stack init_stack();
bool isempty(Stack list);
bool isfull(Stack list, int N);
int getlen(Stack list);
bool push_stack(Stack list, int, int max);
bool pop_stack(Stack list, int* );
int gettop(Stack list);
void destory_stack(Stack list);
int main(){
//M:栈深, N:序列长度, K:序列个数
int M, N, K;
scanf("%d%d%d",&M,&N,&K);
for (int i = 0;i<K;i++){
while(getchar()!='\n')//如果跳出,清理剩余输入
continue;
Stack list = init_stack();
int* temp;
int current;
bool Noway = false;//用于表示栈满,直接失败的情况
temp = (int*)malloc(N * sizeof(int));
for(int j = 0;j<N;j++){
scanf("%d",temp+j);
//第一个数,则先将栈内push到第一个数
if(j==0){
for(current = 1; current<=*(temp+j); current++){
//如果栈满,直接退出循环
if(! push_stack(list, current, M)){
Noway = true;goto here;
}
}
int out;
pop_stack(list,&out);
}
else{
//更新top
int top = gettop(list);
//如top=第一个出栈的数,则出栈
if(top == *(temp+j)){
int out;
pop_stack(list,&out);
}
//如检测的数>top,且其>current,则入栈到current
else if(*(temp+j) > top && *(temp+j)>=current){
while(current <= *(temp+j)){
//如果栈满,直接退出循环
if(! push_stack(list, current, M)){
Noway = true;break;
}
current++;
}//while
int out;
pop_stack(list,&out);
}//elseif
else{
Noway = true;break;
}
}//else
}//for 一行数判断完
here:if(!isempty(list) || Noway)
printf("NO");
else
printf("YES");
if(i!=K-1)
putchar('\n');
free(temp);
destory_stack(list);
}
return 0;
}
/*************************************************************
*****************----------functions---------*****************
*************************************************************/
//初始化栈
Stack init_stack()
{
Stack list = NULL;
list = (Stack)malloc(sizeof(Node));
list->next = NULL;
return list;
}
//判空栈
bool isempty(Stack list)
{
return (list->next == NULL);
}
//判满栈
bool isfull(Stack list, int max)
{
if(getlen(list) == max)
return true;
else
return false;
}
int getlen(Stack list){
int n = 0;
Node*ptr = list->next;
while(ptr){
n++;
ptr = ptr->next;
}
return n;
}
//压入栈
bool push_stack(Stack list, int a, int max)
{
if(isfull(list,max))
return false;
Node* ptr = NULL;
ptr = (Node*)malloc(sizeof(Node));
ptr->data = a;
ptr->next = list->next;
list->next = ptr;
return true;
}
//弹出栈
bool pop_stack(Stack list, int* item)
{
if(isempty(list))
return false;
Node* ptr = list->next;
*item = ptr->data;
list->next = ptr->next;
free(ptr);
return true;
}
//获取栈顶元素
int gettop(Stack list){
if(!isempty(list))
return list->next->data;
else return -1;
}
//销毁栈
void destory_stack(Stack list){
Node*ptr;
while(! isempty(list)){
ptr = list->next;
list->next = ptr->next;
free(ptr);
}
free(list);
return;
}