线性表链表实现数据并集

本文介绍了一种使用链表实现两组数据合并求并集的方法。通过将一个链表复制到新链表,然后遍历另一个链表,检查元素是否已存在于新链表中,若不存在,则插入。此过程实现了两链表的并集运算。

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

使用链表来实现两组数据的合并(求并集)。主要思想就是在两个链表建立后(分别是1号链表和2号链表),先把其中一个(比如1号链表)复制给一个新链表(称之为3号链表),在对2号链表进行遍历,并把符合要求的数据继续插入3号链表。直至2号链表遍历结束,即完成。
以下是代码:

#include<bits/stdc++.h>
using namespace std;
struct ListNode
{
    int date;
    ListNode *next;
};
class List
{
    ListNode *head;
public:
    List(){head=new ListNode;head->next=NULL;}
    void buildList();
    List hebing(List A,List B);
    void DisPlay();
};
void List::buildList()
{
    ListNode *s,*r;
    r=head;
    while(1)
    {
        int a;
        cin>>a;
        if(a!=0)
        {
            s=new ListNode;
            s->date=a;
            r->next=s;
            r=s;
        }
        else break;
    }
    r->next=NULL;
}
List List::hebing(List A,List B)
{
    List C;
    ListNode *p1=A.head->next;
    ListNode *p2=B.head->next;
    ListNode *s,*r;
    r=C.head;
    while(p1!=NULL)
    {
        s=new ListNode;
        s->date=p1->date;
        r->next=s;
        r=s;
        p1=p1->next;
    }
    r->next=NULL;
    while(p2!=NULL)
    {
        int judge=0;
        ListNode *p3=A.head->next;
        while(p3!=NULL)
        {
            if(p3->date==p2->date)
            {
                judge=1;
                p2=p2->next;
                break;
            }
            else p3=p3->next;
        }
        if(judge==0)
        {
            s=new ListNode;
            s->date=p2->date;
            r->next=s;
            r=s;
            p2=p2->next;
        }
    }
    r->next=NULL;
    return C;
}
void List::DisPlay()
{
    ListNode *p=head->next;
    while(p!=NULL)
    {
        cout<<p->date<<" ";
        p=p->next;
    }
    cout<<endl;
}
int main()
{
    List A,B,C;
    A.buildList();
    B.buildList();
    C=C.hebing(A,B);
    C.DisPlay();
    return 0;
}

需要注意的问题:
对2号链表遍历时,内层循环为遍历已经被复制到3号链表的1号链表。此时应针对1号链表的每个元素遍历2号链表,如果出现相同直接break;如果全部不相同(设置一个judge值进行判断),则把对应的2号链表这个数插入3号链表中。因此每次的内层循环应从头开始,记得初始化指针。

### C++ 实现线性链表数据结构 #### 线性的顺序存储实现 线性的顺序存储是通过一组地址连续的存储单元来存储线性中的各个元素。以下是一个使用C++实现线性顺序存储的示例代码,支持插入、删除和查找等功能。 ```cpp #include <iostream> #include <stdexcept> using namespace std; const int MAX_SIZE = 100; // 最大容量 class SeqList { private: int data[MAX_SIZE]; // 存储线性元素的数组 int length; // 当前线性的长度 public: SeqList() : length(0) {} // 构造函数初始化 // 插入元素到指定位置 bool insert(int pos, int value) { if (length >= MAX_SIZE || pos < 0 || pos > length) return false; for (int i = length; i > pos; --i) { data[i] = data[i - 1]; } data[pos] = value; ++length; return true; } // 删除指定位置的元素 bool remove(int pos) { if (pos < 0 || pos >= length) return false; for (int i = pos; i < length - 1; ++i) { data[i] = data[i + 1]; } --length; return true; } // 查找元素值,返回其位置 int search(int value) const { for (int i = 0; i < length; ++i) { if (data[i] == value) return i; } return -1; // 未找到 } // 打印线性内容 void print() const { for (int i = 0; i < length; ++i) { cout << data[i] << " "; } cout << endl; } }; ``` 上述代码展示了如何使用数组实现线性的顺序存储结构,提供了插入、删除和查找功能[^2]。 --- #### 链表实现 链表是一种动态数据结构,适合频繁插入和删除操作。以下是一个使用C++实现链表的示例代码,同样支持插入、删除和查找等功能。 ```cpp #include <iostream> #include <stdexcept> using namespace std; struct Node { int data; // 数据域 Node* next; // 指针域 Node(int val) : data(val), next(nullptr) {} }; class LinkedList { private: Node* head; // 头指针 public: LinkedList() : head(nullptr) {} // 构造函数初始化 ~LinkedList() { // 析构函数释放内存 Node* current = head; while (current != nullptr) { Node* nextNode = current->next; delete current; current = nextNode; } } // 在链表末尾插入元素 void append(int value) { Node* newNode = new Node(value); if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } // 删除指定值的节点 bool remove(int value) { Node* current = head; Node* prev = nullptr; while (current != nullptr && current->data != value) { prev = current; current = current->next; } if (current == nullptr) return false; // 未找到 if (prev == nullptr) { head = current->next; } else { prev->next = current->next; } delete current; return true; } // 查找指定值,返回是否找到 bool search(int value) const { Node* current = head; while (current != nullptr) { if (current->data == value) return true; current = current->next; } return false; } // 打印链表内容 void print() const { Node* current = head; while (current != nullptr) { cout << current->data << " "; current = current->next; } cout << endl; } }; ``` 上述代码展示了如何使用指针实现链表提供了插入、删除和查找功能[^1]。 --- #### 总结 - 线性的顺序存储适用于需要频繁查找且插入删除较少的场景。 - 链表适用于需要频繁插入和删除的场景,且不需要提前确定存储空间大小。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值