# 子序列问题(线性表)

#include<stdio.h>
#include<malloc.h>
struct node             //定义结构体
{
int data;
struct node *next;
};
struct node *creat(int n)
{
while(n--)  //开辟n个新节点，逐个连到链表上
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);//p该连在q后边吧？
q->next = p;
q = p;

}
q->next = NULL;//链表结束
}
{
struct node *p;
{
}
}

int main()//建两条链表p,q
{
int m,n,count=0;
scanf("%d",&m);
scanf("%d",&n);
while(q != NULL)  //双循环判断p是否是q的子列
{
while(p!=NULL)
{
/***修改代码******/
if(q->data == p->data)
{
count++;
}
else
p = p->next;
/***********************/
}
if(p != NULL)
q = q->next;
else
break;
}
if(count == n)
printf("yes\n");
else
printf("no\n");
destroy(p);
destroy(q);
return 0;
}

m个数表示A序列中的m个数据元素。

n个数表示B序列中的n个数据元素。

yes 或者 no

``````9
12 13 14 15 6 71 18 19 10
5
15 6 71 18 19

``````

``yes``

``````#include<stdio.h>
#include<malloc.h>

struct node
{
int data;
struct node *next;
};

struct node *creat(int n){
while(n--){
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
q->next = p;
q = p;

}
q->next = NULL;
}

int main(){
int m,n,count=0;
scanf("%d",&m);
scanf("%d",&n);
while(q != NULL){
while(p!=NULL){
if(q->data == p->data){
count++;
break;
}
else
p = p->next;
}
if(p != NULL)
q = q->next;
else
break;
}
if(count == n)
printf("yes\n");
else
printf("no\n");
return 0;
}
``````

``````#include<stdio.h>
#include<malloc.h>

struct node
{
int data;
struct node *next;
};

struct node *creat(int n){
while(n--){
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
q->next = p;
q = p;

}
q->next = NULL;
}

int main(){
int m,n,count=0;
scanf("%d",&m);
scanf("%d",&n);
while(q != NULL){
while(p!=NULL){
if(q->data == p->data){
count++;
break;
}
else
p = p->next;
}
if(p != NULL)
q = q->next;
else
break;
}
if(count == n)
printf("yes\n");
else
printf("no\n");
return 0;
}
``````