数据结构习题
题目:
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,按顺序push进入堆栈 1,2 ,3 …N 随机pop 判断对应的pop顺序是否能够得到,能够YES,否则NO。
思路:
-想法一:
最开始想法是根据给出的M N K判断对应的顺序能否实现,这种想法太天真了,想不出来。
-想法二:
查询相关资料,方法为:创建堆栈,依次遍历给出的顺序的每一个数字,进行Push动作,直到栈顶等于对应的数字,Pop开始下一个,如果到堆栈满,则说明无法Pop,返回NO,如果最终能够全部遍历完, 说明能够pop对应序列,返回YES。
对应代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 1000 //题目给出最大值不超过1000
typedef struct Stack* pStack;
struct Stack{
int data;
pStack next;
}; //链表方式,定义节点
int array[MAX][MAX]; //定义二重数组用来存储多组数据
/**
功能:创建链表
返回:链表指针型
*/
pStack CreatStack(){
pStack p;
p = (pStack)malloc(sizeof(struct Stack));
p->data = 0;
p->next = NULL;
return p;
}
/**
功能:判断链表是否为空
参数:链表指针
返回:空 true 不空 false
*/
bool IsEmpty(pStack s){
bool state = true;
if(s->next) state = false;
return state;
}
/**
功能:判断链表满
参数:链表指针
返回:满 1 不满 0
*/
bool IsFull(pStack s,int number){
int count = 0;
bool state =0;
while(s->next){
count++;
s = s -> next;
}
if(number == count) state = 1;
return state;
}
/**
功能:入栈
参数:链表指针的指针,插入元素,堆栈指定长度
返回:成功 1 失败 0
*/
bool PushStack(pStack s,int item,int number){
bool state = 0;
// pStack s =*a;
// pStack b = s;
if(!IsFull(s,number)){
pStack node;
node = (pStack)malloc(sizeof(struct Stack));
node->data = item;
node->next = s->next;
s->next = node;
state = 1;
}
return state;
}
/**
* 功能:出栈
* 参数:链表指针
*/
void PopStack(pStack s){
pStack tem;
tem = s->next;
s->next = tem->next;
free(tem);
}
/**
* 主函数
*/
int main(){
int capacity = 0, length = 0 , numbers = 0;//获取对应的三个的值
scanf("%d %d %d",&capacity,&length,&numbers);
for(int i=1;i<=numbers;i++){
for(int j=1;j<=length;j++){
scanf("%d",&array[i][j]);
}
}
// 依次测试样例的每一个数值,依次遍历样例,array[i][j],
// 不断循环+1,判断首节点是否等于刚插入的节点,
// 等于则pop,,j++,判断首节点是否为array[i][j],不是则Push,否则POp
for(int i=1;i<=numbers;i++){
bool error =false;
int j = 1,a = 1;
pStack s = CreatStack();
while(a<=length){
if(PushStack(s,a++,capacity)){
while(s->next&&s->next->data ==array[i][j]){
PopStack(s);
j++;
}
}else{
error=true;
break;
}
}
if(error)printf("NO\n");
else if(j==length+1) printf("YES\n");
else printf("NO\n");
free(s);
}
system("pause"); //可以注释掉,也可以不注释,根据情况
return 0 ;
}