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;
}