java如何写链表的差集_基于链表的两个集合的差集

描述

给定两个递增的整数集合,分别用链表A和B表示,求出A和B的差集(即仅由在A中出现而不在B中出现的元素所构成的集合),并以同样的形式存储,同时返回该集合的元素个数。要求空间复杂度为O(1)。

输入

多组数据,每组数据有三行,第一行为序列A和B的长度n和m,第二行为序列A的n个元素,第三行为序列B的m个元素(元素之间用空格分隔)。n=0且m=0时输入结束。

输出

对于每组数据输出两行,第一行是A和B的差集,第二行为差集中的元素个数,每个数据之间用空格分隔。

输入样例 1

5 5

1 3 5 7 9

1 2 3 4 5

3 4

1 2 6

2 4 5 7

0 0

输出样例 1

7 9

2

1 6

2

#include

using namespace std;

typedef struct LNode{

int data;

struct LNode *next;

}LNode,*LinkList;

void InitList(LinkList &L)

{

L=new LNode;

L->next=NULL;

return;

}

void CreateList_R(LinkList &L,int n)

{

int i;

LinkList r=L;

LinkList p;

for(i=0;i

{

p=new LNode;

int data;

cin>>data;

p->data=data;

p->next=NULL;

r->next=p;

r=p;

}

return;

}

void Show(LinkList &L)

{

LinkList p=L->next;

while(p){

cout<data;

if(p->next) cout<

p=p->next;

}

cout<

return;

}

int ListLength(LinkList L)

{

LinkList p;

p=L->next; //p指向第一个结点

int i=0;

while(p){//遍历单链表,统计结点数

i++;

p=p->next;

}

cout<

}

void Diff(LinkList &A,LinkList &B)

{

LinkList pc=A,pa=A->next,pb=B->next;

while(pa&&pb)

{

if(pa->data==pb->data)

{

pa=pa->next;

pb=pb->next;

}

else if(pa->data > pb->data)

{

pb=pb->next;

}

else if(pa->data < pb->data)

{

pc->next=pa;

pa=pa->next;

pc=pc->next;

}

if(!pb)pc->next=pa;

}

Show(A);

ListLength(A);

return;

}

void Create2List(LinkList &A,LinkList &B,int &n,int &m)

{

CreateList_R(A,n);

CreateList_R(B,m);

return;

}

int main()

{

int n,m;

cin>>n>>m;

while(n!=0&&m!=0)

{

LinkList A,B;

InitList(A);

InitList(B);

Create2List(A,B,n,m);

Diff(A,B);

cin>>n>>m;//key

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个用链表和 C 语言实现计算集合的交集、并集、差集和补集的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结点 typedef struct Node { int value; struct Node *next; } Node; // 定义链表头结点 typedef struct { Node *head; Node *tail; } List; // 初始化链表 void initList(List *list) { list->head = NULL; list->tail = NULL; } // 在链表尾部插入一个新结点 void insert(List *list, int value) { Node *newNode = (Node *) malloc(sizeof(Node)); newNode->value = value; newNode->next = NULL; if (list->head == NULL) { list->head = newNode; } else { list->tail->next = newNode; } list->tail = newNode; } // 判断链表中是否存在某个元素 int find(List *list, int value) { Node *p = list->head; while (p != NULL) { if (p->value == value) { return 1; } p = p->next; } return 0; } // 链表的交集运算 List intersection(List *list1, List *list2) { List result; initList(&result); Node *p1 = list1->head; while (p1 != NULL) { if (find(list2, p1->value)) { insert(&result, p1->value); } p1 = p1->next; } return result; } // 链表的并集运算 List unionSet(List *list1, List *list2) { List result; initList(&result); Node *p1 = list1->head; while (p1 != NULL) { insert(&result, p1->value); p1 = p1->next; } Node *p2 = list2->head; while (p2 != NULL) { if (!find(&result, p2->value)) { insert(&result, p2->value); } p2 = p2->next; } return result; } // 链表差集运算 List difference(List *list1, List *list2) { List result; initList(&result); Node *p1 = list1->head; while (p1 != NULL) { if (!find(list2, p1->value)) { insert(&result, p1->value); } p1 = p1->next; } return result; } // 链表的补集运算 List complement(List *list1, List *list2) { List result; initList(&result); Node *p1 = list1->head; while (p1 != NULL) { if (!find(list2, p1->value)) { insert(&result, p1->value); } p1 = p1->next; } Node *p2 = list2->head; while (p2 != NULL) { if (!find(list1, p2->value)) { insert(&result, p2->value); } p2 = p2->next; } return result; } // 打印链表中的元素 void printList(List *list) { Node *p = list->head; while (p != NULL) { printf("%d ", p->value); p = p->next; } printf("\n"); } int main() { List list1, list2; initList(&list1); initList(&list2); insert(&list1, 1); insert(&list1, 2); insert(&list1, 3); insert(&list2, 2); insert(&list2, 3); insert(&list2, 4); printf("List 1: "); printList(&list1); printf("List 2: "); printList(&list2); printf("Intersection: "); List result1 = intersection(&list1, &list2); printList(&result1); printf("Union: "); List result2 = unionSet(&list1, &list2); printList(&result2); printf("Difference (list1 - list2): "); List result3 = difference(&list1, &list2); printList(&result3); printf("Complement: "); List result4 = complement(&list1, &list2); printList(&result4); return 0; } ``` 在上述代码中,我们定义了链表结点类型 `Node` 和链表类型 `List`,其中 `List` 包含链表头结点和尾结点。我们使用 `initList` 函数初始化链表,使用 `insert` 函数在链表尾部插入一个新结点。`find` 函数用于判断链表中是否存在某个元素。 我们实现了四个函数:`intersection`、`unionSet`、`difference` 和 `complement`,分别用于计算集合的交集、并集、差集和补集。这些函数都返回一个链表类型的结果。最后,我们使用 `printList` 函数打印链表中的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值