链表Set_LinkList(并集)

并集是把两个集合合并,去除重复元素后组成的集合。

通过键盘输入将两个集合保存在链表A和链表B中,再创建一个链表C用于保存集合A、B的并集,将链表C打印输出。

提示:

1.相对于上一题(链表Set_LinkList(建立)),本题只需遍历已保存的链表A、B,将遍历到的当前值插入到链表C中;

2.为遍历取出链表数据,需要编写取值函数GetElem(LinkList L, int i),该函数功能为按序号返回数据元素,注意序号从1开始;

3.遍历链表需要知道链表长度,请自行编写ListLength()函数。

4.用户从键盘输入集合A、B,输入数据为整数,输入以-1结束。

输入格式:

第一行输入集合A,第二行输入集合B。

输出格式:

输出集合A、B的并集C

输入样例:

在这里给出一组输入。例如:

复制代码
9 4 0 7 3 6 5 -1
1 5 6 8 4 7 0 -1

输出样例:

在这里给出相应的输出。例如:

复制代码
9 4 0 7 3 6 5 1 8

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct node{
    ElemType Data;
    struct node* next;
};
typedef struct node* Set_LinkList;
Set_LinkList init(){
    Set_LinkList L = (Set_LinkList)malloc(sizeof(struct node));
    L->Data = -1;
    L->next = NULL;
    return L;
}
int ListLength(Set_LinkList L){
    int counts = 0;
    Set_LinkList p = L->next;
    while(p != NULL){
        counts++;
        p = p->next;
    }
    return counts;
}
ElemType GetElem(Set_LinkList L, int i){
    int counts = 0;
    Set_LinkList p = L->next;
    while(p != NULL){
        counts++;
        if(counts == i){
            return p->Data;
        }
        p = p->next;
    }
    return -1;
}
void CreatList(Set_LinkList L, ElemType x){
    Set_LinkList p = L->next;
    if(p == NULL){
        Set_LinkList newnode = (Set_LinkList)malloc(sizeof(struct node));
        newnode->Data = x;
        newnode->next = NULL;
        L->next = newnode;
        return;
    }
    while(p->next != NULL){
        p = p->next;
    }
    Set_LinkList newnode = (Set_LinkList)malloc(sizeof(struct node));
    newnode->Data = x;
    newnode->next = NULL;
    p->next = newnode;
    return;
}
void display(Set_LinkList L){
    Set_LinkList p = L->next;
    if(p == NULL){
        cout<<"NULL";
        return;
    }
    while(p->next != NULL){
        cout<<p->Data<<' ';
        p = p->next;
    }
    cout<<p->Data;
}
void insert(Set_LinkList L, ElemType e){
    Set_LinkList p = L->next;
    if(p == NULL){
        Set_LinkList newnode = (Set_LinkList)malloc(sizeof(struct node));
        L->next = newnode;
        newnode->Data = e;
        newnode->next = NULL;
        return;
    }
    while(p->next != NULL){
        if(p->Data == e){
            return;
        }
        p = p->next;
    }
    if(p->Data == e){
        return;
    }
    Set_LinkList newnode = (Set_LinkList)malloc(sizeof(struct node));
    p->next = newnode;
    newnode->Data = e;
    newnode->next = NULL;
    return;
}
int main(){
    int x;
    Set_LinkList A = init();
    Set_LinkList B = init();
    cin>>x;
    while(x != -1){
        CreatList(A , x);
        cin>>x;
    }
    cin>>x;
    while(x != -1){
        CreatList(B , x);
        cin>>x;
    }
    int longA = ListLength(A);
    int longB = ListLength(B);
    Set_LinkList C = init();
    for(int i = 1;i <= longA;i++){
        insert(C,GetElem(A,i));
    }
    for(int i = 1;i <= longB;i++){
        insert(C,GetElem(B,i));
    }
    display(C);
    return 0;
}
相关推荐
.30-06Springfield28 分钟前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习
我不是哆啦A梦28 分钟前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt
xiaolang_8616_wjl32 分钟前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
small_wh1te_coder39 分钟前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c
挺菜的1 小时前
【算法刷题记录(简单题)002】字符串字符匹配(java代码实现)
java·开发语言·算法
凌肖战5 小时前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
88号技师5 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
ゞ 正在缓冲99%…6 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
Kaltistss7 小时前
98.验证二叉搜索树
算法·leetcode·职场和发展
知己如祭7 小时前
图论基础(DFS、BFS、拓扑排序)
算法