实验四:求两个集合LA和LB(用单链表表示)的并和交集

本文介绍如何使用单链表表示的集合A和B,通过编写算法来求解它们的交集和并集,并提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构实验4

实验四:求两个集合LA和LB(用单链表表示)的并和交集
要求:在实验二的基础上,使用单链表表示集合。编写两个算法(求交算法和求并算法),并输出最终的结果。

测试用例:集合A为{3,4,1,6},集合B为{2,3,6,7},
交集为{3,6}
并集为{1,2,3,4,6,7}

BZ:暴力!暴力!!暴力!!!(本来想着直接暴力的,但是,,,,我哭了!)
单链表排序(冒泡实现):

前提:

typedef struct student
{
    int data;
    struct student *next;
} Node, *Lin;

冒泡一下:

Lin maopao(Lin head)
{
    if(head == NULL)
        return NULL;
    for(Lin node1=head->next; node1->next!=NULL; node1=node1->next)
    {
        for(Lin node2=head->next; node2->next!=NULL; node2=node2->next)
        {
            if(node2->data > node2->next->data)
            {
                int tem = node2->data;
                node2->data = node2->next->data;
                node2->next->data = tem;
            }
        }
    }
    return head;
}

有序链表A、B求交集:

void jiao(Lin A, Lin B)
{
    Lin q, p, La, Lb;
    La = A->next;
    Lb = B->next;
    int flag = 1;
    while(La && Lb)
    {
        if(La->data == Lb->data)
        {
            flag = 0;
            cout << La->data << ' ';
            La = La->next;
            Lb = Lb->next;
        }
        else if(La->data > Lb->data)
        {
            Lb = Lb->next;
        }
        else
        {
            La = La->next;
        }
    }
    if(flag)
        cout << "A、B集合无交集" << '\n';
    cout << '\n';
}

暴力求并集:

Lin bing(Lin A, Lin B)
{
    Lin q, p, La, Lb;
    La = A->next;
    Lb = B->next;
    while(La!=NULL && Lb!=NULL)
    {
        if(La->data == Lb->data)
        {
            cout << La->data << ' ';
            La = La->next;
            Lb = Lb->next;
        }
        else if(La->data > Lb->data)
        {
            cout << Lb->data << ' ';
            Lb = Lb->next;
        }
        else if(La->data < Lb->data)
        {
            cout << La->data << ' ';
            La = La->next;
        }
    }
    while(La != NULL)
    {
        printf("%d ", La->data);
        La = La->next;
    }
    while(Lb != NULL)
    {
        printf("%d ", Lb->data);
        Lb = Lb->next;
    }
    cout << '\n';
}

实验四总代码:

//#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<math.h>
using namespace std ;
typedef long long ll;
#define MAXN 100005
#define INF 0x3f3f3f3f
#define MALL (Node *)malloc(sizeof(Node));

typedef struct student
{
    int data;
    struct student *next;
} Node, *Lin;

void print(Lin head)//输出链表
{
    Lin p = head->next;
    while(p != NULL)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    cout << '\n';
}

Lin Creat(int n)//创建链表
{
    Lin head, q, p;
    head = MALL;
    head->next = NULL;
    q = head;
    for(int i=0; i<n; ++i)
    {
        p = MALL;
        scanf("%d", &(p->data));
        p->next = NULL;
        q->next = p;
        q = p;
    }
    return head;
}

Lin maopao(Lin head)//链表排序
{
    if(head == NULL)
        return NULL;
    for(Lin node1=head->next; node1->next!=NULL; node1=node1->next)
    {
        for(Lin node2=head->next; node2->next!=NULL; node2=node2->next)
        {
            if(node2->data > node2->next->data)
            {
                int tem = node2->data;
                node2->data = node2->next->data;
                node2->next->data = tem;
            }
        }
    }
    return head;
}

void jiao(Lin A, Lin B)
{
    Lin q, p, La, Lb;
    La = A->next;
    Lb = B->next;
    int flag = 1;
    while(La && Lb)
    {
        if(La->data == Lb->data)
        {
            flag = 0;
            cout << La->data << ' ';
            La = La->next;
            Lb = Lb->next;
        }
        else if(La->data > Lb->data)
        {
            Lb = Lb->next;
        }
        else
        {
            La = La->next;
        }
    }
    if(flag)
        cout << "A、B集合无交集" << '\n';
    cout << '\n';
}

Lin bing(Lin A, Lin B)
{
    Lin q, p, La, Lb;
    La = A->next;
    Lb = B->next;
    while(La!=NULL && Lb!=NULL)
    {
        if(La->data == Lb->data)
        {
            cout << La->data << ' ';
            La = La->next;
            Lb = Lb->next;
        }
        else if(La->data > Lb->data)
        {
            cout << Lb->data << ' ';
            Lb = Lb->next;
        }
        else if(La->data < Lb->data)
        {
            cout << La->data << ' ';
            La = La->next;
        }
    }
    while(La != NULL)
    {
        printf("%d ", La->data);
        La = La->next;
    }
    while(Lb != NULL)
    {
        printf("%d ", Lb->data);
        Lb = Lb->next;
    }
    cout << '\n';
}

int main()
{
    while(1)
    {
        Lin A, B;
        cout << "请输入链表A的个数n和各元素" << '\n';
        int n, m;
        cin >> n;
        A = Creat(n);
        cout << "请输入链表B的个数m和各元素" << '\n';
        cin >> m;
        B = Creat(m);

        cout << "此时的A、B链表为:" << '\n';
        cout << "A: ";
        print(A);
        cout << "B: ";
        print(B);

        cout << "下面对A、B进行冒泡排序" << '\n';
        A = maopao(A);//排序
        B = maopao(B);

        cout << "此时的A、B链表排序后为: " << '\n';
        cout << "A: ";
        print(A);
        cout << "B: ";
        print(B);
        while(1)
        {
            cout << "请选择你的操作:" << '\n';
            cout << "1、合并A、B后输出(即输出A、B的并集)" << '\n';
            cout << "2、求A、B的交集并输出" << '\n';
            cout << "3、退出操作" << '\n';

            int a;
            cin >> a;
            if(a == 1)
            {
                bing(A, B);
            }
            else if(a == 2)
            {
                jiao(A, B);
            }
            else
                break;
            system("pause");
            system("cls");
        }
        cout << "A、继续创建A、B链表" << '\n';
        cout << "B、退出程序" << '\n';
        char ch;
        cin >> ch;
        if(ch == 'B')
            break;
        else
        {
            system("pause");
            system("cls");
        }
    }
    return 0;
}


好恶心啊!!!下次我可以暴力坚决不多想。。。。。。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值