求两个单链表的差集和并集

11 篇文章 0 订阅

题目:-
已知集合A和B的元素分别用不含头结点的单链表存储,函数difference()用于求解集合A与B的差集,并将结果保存在集合A的单链表中。例如,若集合A={5,10,20,15,25,30},集合B={5,15,35,25},完成计算后A={10,20,30}。
这里写图片描述
思路:指针的指针pa遍历链表A,指针pb遍历链表B,当节点的值相等,删除,提前将删除位置的下一个节点保存,然后
继续遍历这两个链表直到全部遍历完后,剩下的节点连接起来就是这两个链表的差集
验证:

#include<iostream>
#include<assert.h>
using namespace std;
struct Node
{
    int _data;
    Node*next;

    Node(int data)
        :_data(data)
        , next(NULL)
    {}
};
void Different(Node**A, Node*B)
{
    Node*pa = *A;//用来遍历A链表
    //Node*pb = B;//用来遍历B链表
    Node *prev = NULL;//用来保存删除后节点能连接起来
    Node*del;//待删除的节点
    while (pa)
    {
        Node*pb = B;
        while (pb&&pa->_data != pb->_data)
            pb = pb->next;//
        if (pb)//两个节点的值相等
        {
            if (!prev)
            {
                *A = pa->next;//指向下一个节点
            }
            else//第一个节点相等
            {
                prev->next = pa->next;//指向相等节点的后一个
            }
            del = pa;//删除的节点
            pa = pa->next;
            delete del;
        }
        //此时链表B已经遍历完了
        else
        {
            //更新节点的位置
            prev = pa;
            pa = pa->next;
        }
    }
}
void Printf(Node*root)
{
    Node*cur = root;
    while(cur)
    {
        cout << cur->_data << " ";
        cur = cur->next;
    }
    cout << endl;
}
void Test()
{
    Node*n1 = new Node(5);
    Node*n2 = new Node(10);
    Node*n3 = new Node(20);
    Node*n4 = new Node(15);
    Node*n5 = new Node(25);
    Node*n6 = new Node(30);

    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    n5->next = n6;
    cout << "链表A" << endl;
    Printf(n1);

    Node*n11 = new Node(5);
    Node*n22 = new Node(15);
    Node*n33 = new Node(35);
    Node*n44 = new Node(25);

    n11->next = n22;
    n22->next = n33;
    n22->next = n33;
    n33->next = n44;
    cout << "链表B" << endl;

    Printf(n11);
    cout << "链表的差集" << endl;

    Different(&n1, n11);
    Printf(n1);
}

int main()
{
    Test();
    system("pause");
    return 0;
}

这里写图片描述
求交集
同样的方法,我们可以求两个单链表的交集
思路:当链表A的值小于链表B的值,链表A相后移,如果链表A的值大于链表B的值,链表B的值向后移,否则同时移,当遍历完这个链表,交集就出来了.
验证

void UnionSet(Node*L1, Node*L2)
{
    while(L1&&L2)
    {
        if (L1->_data < L2->_data)
        {
            L1 = L1->next;
        }
        else if (L1->_data>L2->_data)
        {
            L2 = L2->next;
        }
        else
        {
            L1 = L1->next;
            L2 = L2->next;
        }
    }
}
void Printf(Node*root)
{
    Node*cur = root;
    while(cur)
    {
        cout << cur->_data << " ";
        cur = cur->next;
    }
    cout << endl;
}
void Test()
{
    Node*n1 = new Node(5);
    Node*n2 = new Node(10);
    Node*n3 = new Node(20);
    Node*n4 = new Node(15);
    Node*n5 = new Node(25);
    Node*n6 = new Node(30);

    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    n5->next = n6;
    cout << "链表A" << endl;
    Printf(n1);

    Node*n11 = new Node(5);
    Node*n22 = new Node(15);
    Node*n33 = new Node(35);
    Node*n44 = new Node(25);

    n11->next = n22;
    n22->next = n33;
    n22->next = n33;
    n33->next = n44;
    cout << "链表B" << endl;

    Printf(n11);


    cout << "链表的交集" << endl;
    UnionSet(n1, n11);
    Printf(n1);


}

int main()
{
    Test();
    system("pause");
    return 0;
}
  • 8
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 初始化集合 定义一个结构体Node,用于存储每个元素和指向下一个元素的指针。定义一个链表头指针head,初始为NULL,表示链表为空。 struct Node { int data; Node* next; }; Node* head = NULL; 2. 按指定位置插入一个数到集合 先判断要插入的位置是否合法,即是否在链表长度范围内。然后遍历链表,找到要插入位置的前一个节点,将要插入的节点插入到该节点之后。 void insert(int pos, int value) { if (pos < 1 || pos > size() + 1) { cout << "Invalid position" << endl; return; } Node* newNode = new Node; newNode->data = value; newNode->next = NULL; if (pos == 1) { newNode->next = head; head = newNode; } else { Node* pre = head; for (int i = 1; i < pos - 1; i++) { pre = pre->next; } newNode->next = pre->next; pre->next = newNode; } } 3. 按值删除集合中的元素 先判断链表是否为空。然后遍历链表,找到要删除的节点的前一个节点,将该节点从链表中删除并释放内存。 void remove(int value) { if (head == NULL) { cout << "The set is empty" << endl; return; } Node* pre = NULL; Node* cur = head; while (cur != NULL && cur->data != value) { pre = cur; cur = cur->next; } if (cur == NULL) { cout << "The value is not in the set" << endl; return; } if (pre == NULL) { head = cur->next; } else { pre->next = cur->next; } delete cur; } 4. 按值在集合中进行查找 遍历链表,找到第一个值为value的节点,返回该节点的位置。 int find(int value) { Node* cur = head; int pos = 1; while (cur != NULL && cur->data != value) { cur = cur->next; pos++; } if (cur == NULL) { return -1; } else { return pos; } } 5. 清空集合 遍历链表,释放每个节点的内存,将链表头指针置为NULL。 void clear() { Node* cur = head; while (cur != NULL) { Node* temp = cur; cur = cur->next; delete temp; } head = NULL; } 6. 两个集合的交集 先新建一个空集合,遍历第一个集合,对于每个元素,在第二个集合中查找是否存在,如果存在则插入到新集合中。 Node* intersect(Node* set1, Node* set2) { Node* newSet = NULL; Node* cur1 = set1; while (cur1 != NULL) { if (findInSet(cur1->data, set2) != -1) { insertToSet(cur1->data, newSet); } cur1 = cur1->next; } return newSet; } 7. 两个集合的并集 先新建一个空集合,遍历两个集合,将所有元素插入到新集合中。插入时需要判断新集合中是否已经存在该元素。 Node* unionSet(Node* set1, Node* set2) { Node* newSet = NULL; Node* cur1 = set1; while (cur1 != NULL) { insertToSet(cur1->data, newSet); cur1 = cur1->next; } Node* cur2 = set2; while (cur2 != NULL) { if (findInSet(cur2->data, newSet) == -1) { insertToSet(cur2->data, newSet); } cur2 = cur2->next; } return newSet; } 8. 两个集合的差集 先新建一个空集合,遍历第一个集合,对于每个元素,在第二个集合中查找是否存在,如果不存在则插入到新集合中。 Node* difference(Node* set1, Node* set2) { Node* newSet = NULL; Node* cur1 = set1; while (cur1 != NULL) { if (findInSet(cur1->data, set2) == -1) { insertToSet(cur1->data, newSet); } cur1 = cur1->next; } return newSet; } 9. 输出集合 遍历链表,依次输出每个元素。 void print(Node* set) { Node* cur = set; while (cur != NULL) { cout << cur->data << " "; cur = cur->next; } cout << endl; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值