7-5 两个有序链表序列的交集 (20分) 已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。 输入格式: 输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−

7-5 两个有序链表序列的交集 (20分)

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。

输入格式:

输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。

输出格式:

在一行中输出两个输入序列的交集序列,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL

输入样例:

1 2 5 -1
2 4 5 8 10 -1

输出样例:

2 5
#include "stdio.h"
#include "math.h"
#define N 100000
typedef struct linknode
{
    int data;
    struct linknode *next;
    
}node,*ptr;

int main(){
    ptr s1,s2,s3,p,q,z,last;
    int x;
    s1 = NULL;
    s2=NULL;
    s3=NULL;

    
 
    /*  前向插入
     while (x>=0) {
        p=(ptr)malloc(sizeof(node));
        p->data=x;
        //前向插入足矣
        p->next=head;
        head=p;
        scanf("%d",&x);
    }*/
    //后向插入方法
    scanf("%d",&x);
    while(x>=0){
        p=(ptr)malloc(sizeof(node));
        p->data=x;
        if (s1==NULL) {
            p->next=s1;s1=p;last=p;
        }else{
            last->next=p;p->next=NULL;last=p;
        }
        scanf("%d",&x);//read next
    }
    
    scanf("%d",&x);
    while (x>=0) {
        p=(ptr)malloc(sizeof(node));
        p->data=x;
        if (s2==NULL) {
            p->next=s2;s2=p;last=p;
        }else{
            last->next=p;p->next=NULL;last=p;
        }
        scanf("%d",&x);
    }
    
//    p = s1;
//    while (p!=NULL) {
//        printf("%d",p->data);
//        p=p->next;
//    }
//    p = s2;
//    while (p!=NULL) {
//        printf("%d",p->data);
//        p=p->next;
//    }
//
    s3=(ptr)malloc(sizeof(node));
    s3->next=NULL;
    for (p=s1,q=s2,z=s3; p!=NULL && q!=NULL; ) {
        if (p->data==q->data) {
            z->next=p;
            z=z->next;
            //printf("%d",p->data);
            p=p->next;
            q=q->next;
        }else if (p->data<q->data){
            p=p->next;
        }else if (p->data>q->data){
            q=q->next;
        }
    }
     
    if ((s3->next==NULL||s1==NULL)||s2==NULL) {
        printf("NULL");
    }else{
        p = s3->next;
        while (p!=NULL) {
            printf("%d",p->data);
            if (p->next!=NULL) {
                printf(" ");
            }
            p=p->next;
        }
    }

//    p = s1;
//    while (p!=NULL) {
//        printf("%d",p->data);
//        p=p->next;
//    }

    
    
}

 

在C语言中,要设计一个函数来构建两个非降序链表S1和S2交集链表S3,可以采用以下步骤: 1. 定义结构体`ListNode`表示链表节点,包含数据域`data`和指针域`next`。 ```c typedef struct ListNode { int data; struct ListNode* next; } ListNode; ``` 2. 创建一个辅助函数`isInList`,用于检查一个元素是否在给定链表中。 ```c bool isInList(ListNode* list, int target) { while (list != NULL) { if (list->data == target) return true; list = list->next; } return false; } ``` 3. 主函数`mergeLists`中,初始化一个空链表`result`作为结果,并遍历第一个链表S1。 ```c ListNode* mergeLists(ListNode* S1, ListNode* S2) { ListNode* result = NULL; // 新链表头结点 ListNode* current1 = S1; ListNode* current2 = S2; // 遍历S1 while (current1 != NULL && current2 != NULL) { if (current1->data < current2->data) { // 如果S1的当前元素不在S2中,则添加到结果 if (!isInList(current2, current1->data)) { if (result == NULL) result = current1; else result->next = current1; current1 = current1->next; } else { // S1的当前元素也在S2中,跳过 current1 = current1->next; } } else { // 相同或S2的当前元素小于S1 if (!isInList(current1, current2->data)) { if (result == NULL) result = current2; else result->next = current2; current2 = current2->next; } else { // S2的当前元素也在S1,跳过 current2 = current2->next; } } } // 将剩余部添加至结果链表 while (current1 != NULL || current2 != NULL) { if (current1 != NULL) result->next = current1, current1 = current1->next; else result->next = current2, current2 = current2->next; } return result; } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值