首先,将对2个链表进行排序,然后遍历2个链表,得到2个了表 的交集和并集。 下面是具体实现步骤: 用归并排序对第1个链表进行排序,这个操作的时间复杂度为O(mLogm). 用归并排序堆第2个链表进行排序,这个操作的时间复杂度为O(nLogn). 线性遍历2个有序的链表,得到2个链表的交集和并集。这个操作的时间复杂度为O(...
printf("A与B的交集:共%d个\n",k-1); for(i=1;i<k;i++){printf("%d ",c[i]);}printf("\n"); printf("A与B的并集:共%d个\n",x+y+k-3); for(i=1;i<x;i++){printf("%d ",aa[i]);} for(i=1;i<y;i++){printf("%d ",bb[i]);} for(i=1;i<k;i++){printf("%d...
qsort(B, m,sizeof(B[0]), cmp);// 集合B元素升序排列 for(i = 0, j = 0, k = 0; i < n && j < m; ) {// i、j任一指针遍历完集合时结束循环 if(A[i] == B[j]) {// 当i, j两指针指向元素相等 C[k++] = A[i];// 元素进交集C i++; j++;// 两指针均移动 } else{...
} /* 交集 *//* A与B的交集(A∩B):既属于A又属于B的元素构成的集合 */int setIntersection (set A, set B, set *dest) {int i = 0, j = 0, k = 0;dest->length = 0;for (i=0; i<A.length; i++) { /* 外循环遍历A */for (j=0; j<B.length; j++) { /* ...
以前写过一个纯C的, 用的是数组,模拟C++ STL里面的set_intersection,set_union和set_difference的实现。 稍作了修改,添加了些注释,希望能帮到你。注意:必须先对输入集合排序;输出结果和C++ STL的测试结果吻合。include <stdio.h>#include <stdlib.h>#include <string.h>int set_intersection (...
C语⾔数据结构之求两个集合的交集(链表)//1:求两集合的交集(链表)。#include <stdio.h> #include <stdlib.h> struct node { int data;struct node* next;};void push(struct node **head_ref, int new_data); //添加数据元素声明 bool isPresent(struct node *head, int data); //判断是否存在...