/*两个整数序列A=a1,a2,a3,...,am和B=b1,b2,b3,...,bn已存入两个单链表中,设计一个算法,判断
序列B是否是序列A的连续子序列*/
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList createHeadList(LinkList &L)
{
LNode *r;
L = (LinkList)malloc(sizeof(LNode));
r = L;
ElemType x;
scanf("%d",&x);
while(x != 9999)
{
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d",&x);
}
r->next = NULL;
return L;
}
bool judgeChild(LinkList A,LinkList B)
{
LNode *pre = A;
LNode *p = pre->next;
LNode *q = B->next;
while(p && q)
{
if(p->data == q->data)
{
p = p->next;
q = q->next;
}
else{
pre = pre->next;
p = pre->next;
q = B->next;
}
}
if(q == NULL)
return true;
else
return false;
}
bool printList(LinkList L)
{
LNode *p = L->next;
while(p)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return true;
}
void main()
{
LinkList A,B;
bool flag;
printf("创建链表A:");
createHeadList(A);
printf("创建链表B:");
createHeadList(B);
flag = judgeChild(A,B);
if(flag)
printf("yes\n");
else
printf("no\n");
}
判断是否为子序列
最新推荐文章于 2022-05-09 18:56:14 发布