基础实验3-2.4 出栈序列的合法性 (25分)
给定一个最大容量为 M 的堆栈,将 N 个数字按 1, 2, 3, …, N 的顺序入栈,允许按任何顺序出栈,则哪些数字序列是不可能得到的?例如给定 M=5、N=7,则我们有可能得到{ 1, 2, 3, 4, 5, 6, 7 },但不可能得到{ 3, 2, 1, 7, 5, 6, 4 }。
输入格式:
输入第一行给出 3 个不超过 1000 的正整数:M(堆栈最大容量)、N(入栈元素个数)、K(待检查的出栈序列个数)。最后 K 行,每行给出 N 个数字的出栈序列。所有同行数字以空格间隔。
输出格式:
对每一行出栈序列,如果其的确是有可能得到的合法序列,就在一行中输出YES,否则输出NO。
输入样例:
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
输出样例:
YES
NO
NO
YES
NO
- 很奇怪,自己跑的时候例程都是对的,放到PTA上就有问题,不知道有没有DL能够看出问题在哪里。
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define ERROR -1
typedef int ElementType;
typedef struct SNode * PrtToSNode;
struct SNode
{
ElementType Data;
PrtToSNode Next;
int MaxSize;
};
typedef PrtToSNode Stack;
typedef PrtToSNode Position;
typedef struct QNode * PrtToQNode;
struct QNode
{
Position Front, Rear;
};
typedef PrtToQNode Queue;
// Stack
Stack CreateStack(int M)
{
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Next = NULL;
S->MaxSize = M;
return S;
}
bool SIsEmpty(Stack S)
{
return (S->Next == NULL);
}
bool SIsFull(Stack S, int M)
{
return (S->MaxSize == M);
}
bool Push(Stack S, ElementType X)
{
PrtToSNode TmpCell;
TmpCell = (PrtToSNode)malloc(sizeof(struct SNode)); // create a new node
TmpCell->Data = X; // save the value
TmpCell->Next = S->Next; // link the new node
TmpCell->MaxSize = S->MaxSize;
S->Next = TmpCell;
return true;
}
ElementType Pop(Stack S)
{
PrtToSNode FirstCell;
ElementType TopElem;
if(SIsEmpty(S))
{
printf("Stack Empty.");
return ERROR;
}
else
{
FirstCell = S->Next; // We get the pre-stored node
TopElem = FirstCell->Data; // get the value
S->Next = FirstCell->Next; //update the node position
free(FirstCell); // free the space
return TopElem; // return the value
}
}
// Queue
bool QisEmpty(Queue Q)
{
return (Q->Front == NULL);
}
Queue CreateQueue(void)
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
if(!Q)
{
printf("Queue malloc failed.\n");
return NULL;
}
Q->Front = Q->Rear = NULL;
return Q;
}
void InQueue(Queue Q, ElementType Data)
{
Position n = (Position)malloc(sizeof(struct SNode));
if (NULL == n)
{
printf("InQueue malloc failed");
return;
}
n->Data = Data;
n->Next = NULL;
if(Q->Rear == NULL)
{
Q->Front = n;
Q->Rear = n;
}
else
{
Q->Rear->Next = n;
Q->Rear = n;
}
}
ElementType DeleteQ(Queue Q)
{
Position FrontCell;
ElementType FrontElem;
if(QisEmpty(Q))
{
printf("Queue is empty.");
return ERROR;
}
else
{
FrontCell = Q->Front;
if(Q->Front == Q->Rear)
Q->Front = Q->Rear = NULL;
else
Q->Front = Q->Front->Next;
FrontElem = FrontCell->Data;
free(FrontCell);
return FrontElem;
}
}
bool Check(Stack S, Queue Q, int N)
{
int i = 0;
int PushNum = 1;
int StackNum = 1;
// if the number which from the Queue is bigger than the Pop number
// and the stack is not full, we need to push the number that is lower than is number,
// because the value of the number indicates that all numbers that are lower than this
// value have been pushed into the stack
// if the number is lower than the Pop number, there exists a paradox. Because all the numbers
// that are lower than this value had been pushed into the stack.
Push(S, 0); // we need a sentry
while(i++ < N)
{
ElementType out_Queue = DeleteQ(Q);
while(out_Queue > S->Next->Data && !SIsFull(S, StackNum))
{
Push(S, PushNum++);
StackNum++;
}
if(out_Queue == S->Next->Data)
{
Pop(S);
StackNum--;
}
else
return false;
}
// clear Queue and Stack
while(!QisEmpty)
DeleteQ(Q);
while(!SIsEmpty)
Pop(S);
return true;
}
int main(){
int M, N, K, t;
// maximum stack size is 'M', maximum sequence size is 'N', total number
// of the sequences are 'K'
scanf("%d %d %d", &M, &N, &K);
for(int i = 0; i < K; i++){
Queue Q = CreateQueue();
Stack S = CreateStack(M + 1);
// let N number enter the queue
for(int j = 0; j < N; j++)
{
scanf("%d", &t);
InQueue(Q, t);
}
// check the sequence
if(Check(S, Q, N))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}