本文主要是介绍求两个单链表的差集,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
归纳编程学习的感悟,
记录奋斗路上的点滴,
希望能帮到一样刻苦的你!
如有不足欢迎指正!
共同学习交流!
🌎欢迎各位→点赞 👍+ 收藏⭐ + 留言📝
但行前路,不负韶华!
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<malloc.h>
#include"LinkList.h"LNode* Difference(LinkList A, LinkList B);int main()
{LinkList A, B, C;LNode* p;InitList(&A);InitList(&B);DataType a[] = { 22,7,15,56,89,38,44,65,109,83 };DataType b[] = { 15,9,22,89,33,65,90,83 };for (int i = 1; i <= sizeof(a) / sizeof(a[0]); i++){if (InsertElem(A, i, a[i - 1]) == 0){printf("插入位置不合法!\n");return 0;}}for (int i = 1; i <= sizeof(b) / sizeof(b[0]); i++){if (InsertElem(B, i, b[i - 1]) == 0){printf("插入位置不合法!\n");return 0;}}printf("A链表中共有%d个元素:\n", ListLength(A));p = A;while (p->next){p = p->next;printf("%d ", p->data);}printf("\n");printf("B链表中共有%d个元素:\n", ListLength(B));p = B;while (p->next){p = p->next;printf("%d ", p->data);}printf("\n");C = Difference(A, B);printf("两链表的差集链表中共有%d个元素:\n", ListLength(C));p = C;while (p->next){p = p->next;printf("%d ", p->data);}printf("\n");return 0;
}LNode* Difference(LinkList A, LinkList B)
{LNode* p, * q;p = A->next;q = B->next;LinkList C;InitList(&C);while (p){while (q){if (p->data == q->data){break;}q = q->next;}if (q == NULL){InsertElemR(C, p->data);}p = p->next;q = B->next;}return C;
}
这篇关于求两个单链表的差集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!