子序列问题(线性表)

23 篇文章 0 订阅

题目描述

两个整数序列A=a1,a2,a3,…,am和B=b1,b2,b3,…,bn已经存入两个单链表中,设计一个算法,判断序列B是否是序列A的子序列,代码给出如下,请修改~

本题只需提交修改部分
#include<stdio.h>
 #include<malloc.h>
 struct node             //定义结构体
{
     int data;
     struct node *next;
 };
 struct node *creat(int n)
 {
     struct node *head,*p,*q; //head是头节点,p指向新开辟的节点,q指向已连接的上一个节点
    head=(struct node *)malloc(sizeof(struct node));//给head开辟一个节点
    q = head;  //q节点连接到head上
    while(n--)  //开辟n个新节点,逐个连到链表上
    {
         p=(struct node *)malloc(sizeof(struct node));
         scanf("%d",&p->data);//p该连在q后边吧?
        q->next = p;
         q = p;

    }
     q->next = NULL;//链表结束
    return head;
 }
 void destroy(struct node *head)
 {
  struct node *p;
  while(head!=NULL)
  {
   p=head->next;
   delete(head);
   head=p;
  }
 }

int main()//建两条链表p,q
 {
     int m,n,count=0;
     struct node *head1,*head2,*p,*q;
     scanf("%d",&m);
     head1 = creat(m);
     scanf("%d",&n);
     head2 = creat(n);
     q=head2->next;
     p = head1->next;
     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。

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

一个整数n,表示B序列的长度n。

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)
{
    struct node *head,*p,*q; //head是头节点,p指向新开辟的节点,q指向已连接的上一个节点
    head=(struct node *)malloc(sizeof(struct node));//给head开辟一个节点
    q = head;  //q节点连接到head上
    while(n--)  //开辟n个新节点,逐个连到链表上
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);//p该连在q后边吧?
        q->next = p;
        q = p;
 
    }
    q->next = NULL;//链表结束
    return head;
}
int main()//建两条链表p,q
{
    int m,n,count=0;
    struct node *head1,*head2,*p,*q;
    scanf("%d",&m);
    head1 = creat(m);
    scanf("%d",&n);
    head2 = creat(n);
    q=head2->next;
    p = head1->next;
    while(q != NULL)  //双循环判断p是否是q的子列
    {
        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;
}
*/

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值