问题 C 实验11_11_链表匹配

问题 C 实验11_11_链表匹配

题目描述
已知两个由正整数组成的无序序列A、B,每个序列的元素个数未知,但至少有一个元素。你的任务是判断序列B是否是序列A的连续子序列。假设B是“1 9 2 4 18”,A是“33 64 1 9 2 4 18 7”,B是A的连续子序列;假设B是“1 9 2 4 18”,A是“33 1 9 64 2 4 18 7”,B不是A的连续子序列。
要求:
建立两个单链表A、B用于存储两个正整数序列,然后按照题目的要求,判断链表B是否是链表A的连续子序列。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。在程序结束前要释放链表A、B中的所有节点。
输入
依次输入两个乱序的正整数序列A、B,序列中元素个数未知,但每个序列至少有一个元素,并以输入“-1”结束,每个序列占一行。
输出
如果序列B是序列A的连续子序列,则输出“ListB is the sub sequence of ListA.”,否则输出“ListB is not the sub sequence of ListA.”。
数据最多的测试用例节点数在100这个数量级,所有整数可以用int型存储。
请注意输入输出格式。
样例输入 Copy
Sample 1:

5 4 3 2 1 -1
3 2 1 -1

Sample 2:
1 2 3 4 5 6 7 8 9 -1
1 2 3 4 5 6 7 8 0 -1

样例输出 Copy

Sample 1:
ListB is the sub sequence of ListA.

Sample 2:
ListB is not the sub sequence of ListA.
/* creat the linked list and check if ListB is the sub sequence of ListA.
@author: CangCheng
@date: 13/3/2021
*/

#include<stdio.h>
#include <stdlib.h>

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

struct node *creatLinkedList(struct node *headPtr);

void delLinkedList(struct node *headPtr);

int ifBIsSubOfA(struct node *headPtrA, struct node *headPtrB);

int ifBIsInA(struct node *headPtrA, struct node *headPtrB);

void printTheResult(int flag);

int main() {
    struct node *headPtrA = NULL, *headPtrB = NULL;
    int flag;
    headPtrA = creatLinkedList(headPtrA);
    headPtrB = creatLinkedList(headPtrB);
    flag = ifBIsInA(headPtrA, headPtrB);
    if (flag == 1) {
        flag = ifBIsSubOfA(headPtrA, headPtrB);
        printTheResult(flag);
    } else {
        printTheResult(flag);
    }
    delLinkedList(headPtrA);
    delLinkedList(headPtrB);
    return 0;
}

//创建有空节点的链表
struct node *creatLinkedList(struct node *headPtr) {
    struct node *newPtr, *currentPtr;
    int num;
    newPtr = malloc(sizeof(struct node));
    newPtr->nextPtr = NULL;
    headPtr = newPtr;
    scanf("%d", &num);
    while (num != -1) {
        newPtr = malloc(sizeof(struct node));
        newPtr->data = num;
        newPtr->nextPtr = NULL;
        if (headPtr->nextPtr == NULL) {
            currentPtr = newPtr;
            headPtr->nextPtr = currentPtr;
        } else {
            currentPtr->nextPtr = newPtr;
            currentPtr = newPtr;
        }
        scanf("%d", &num);
    }
    return headPtr;
}

//删除链表
void delLinkedList(struct node *headPtr) {
    struct node *thisPtr, *thatPtr;
    thisPtr = headPtr;
    while (thisPtr != NULL) {
        thatPtr = thisPtr->nextPtr;
        free(thisPtr);
        thisPtr = thatPtr;
    }
}

//B是否是A的子序列
int ifBIsInA(struct node *headPtrA, struct node *headPtrB) {
    struct node *thisPtrA, *thisPtrB;
    headPtrA = headPtrA->nextPtr;
    headPtrB = headPtrB->nextPtr;
    thisPtrB = headPtrB;
    while (thisPtrB != NULL) {
        thisPtrA = headPtrA;
        while (thisPtrA != NULL) {
            if (thisPtrB->data == thisPtrA->data) {
                break;
            } else {
                thisPtrA = thisPtrA->nextPtr;
            }
        }
        if (thisPtrA == NULL) {
            return 0;
        }
        thisPtrB = thisPtrB->nextPtr;
    }
    return 1;
}

//打印输出结果
void printTheResult(int flag) {
    if (flag == 1) {
        printf("ListB is the sub sequence of ListA.");
    }
    if (flag == 0) {
        printf("ListB is not the sub sequence of ListA.");
    }
}

//B是否是A的连续子序列
int ifBIsSubOfA(struct node *headPtrA, struct node *headPtrB) {
    struct node *thisPtrA, *thisPtrB, *lastPtr;
    int flag = 0;
    headPtrA = headPtrA->nextPtr;
    headPtrB = headPtrB->nextPtr;
    thisPtrB = headPtrB;
    thisPtrA = headPtrA;
    while (thisPtrA != NULL) {
        if (thisPtrB == NULL) {
            return 1;
        }
        if (flag == 1 && thisPtrA->data != thisPtrB->data) {
            thisPtrB = headPtrB;
            thisPtrA = lastPtr->nextPtr;
            flag = 0;
            continue;
        }
        if (thisPtrA->data == thisPtrB->data) {
            thisPtrB = thisPtrB->nextPtr;
            if (flag == 0) {
                lastPtr = thisPtrA;
            }
            flag = 1;
        }
        thisPtrA = thisPtrA->nextPtr;
        if (thisPtrB == NULL) {
            return 1;
        }
    }
    return 0;
}








//特别注意2 2 2 2 5 2 2 -1    2 2 2 5 2 -1
//特别注意1 2 3 2 3 4 -1       2 3 4 -1
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值