集合的交并差(链表)(未完)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>

using namespace std;

int len1=0;
int len2=0;


typedef struct node{

	int valum;
	struct node *next;

}Node,*Linklist;

void creat_list(Linklist &head,int n)
{

	Linklist p,q;
	head=(Linklist)malloc(sizeof(Node));
	head->next=NULL;
	int i;
	srand(time(0));
	int flag[100]={0};
	for(i=0;i<n;i++)
	{
		p=(Linklist)malloc(sizeof(Node));
		while(1)
		{
			p->valum=rand()%89+10;
			if(flag[p->valum]==0)
			{	
				flag[p->valum]=1;
				break;
			}
		}
		p->next=head->next;
		head->next=p;
		
	}
}

void Display(Linklist head)
{
	int i;
	Linklist p;
	p=head->next;
	while(p)
	{
		if(p->valum)
		{printf("%d ",p->valum);
		p=p->next;}
		else
		p=p->next;
	}
}

Linklist Partion(Linklist &pBegin, Linklist &pEnd)  
{  
    int key = pBegin->valum;  
    Linklist p = pBegin;  
    Linklist q = p->next;  
  
    while(q!=pEnd)  
    {  
        if(q->valum < key)  
        {  
            p = p->next;  
            swap(p->valum,q->valum);  
        }  
  
        q = q->next;  
    }  
    swap(p->valum,pBegin->valum);  
    return p;  
}  
  
void QuickSort(Linklist &pBeign, Linklist pEnd)  
{  
    if(pBeign != pEnd)  
    {  
        Linklist partion = Partion(pBeign,pEnd);  
        QuickSort(pBeign,partion);  
        QuickSort(partion->next,pEnd);  
    }  
}  

void N_list(Linklist &la,Linklist lb)
{
	Linklist p,q;
	p=la->next;
	while(p)
	{
		q=lb->next;
		while(q)
		{
			if(q->valum==p->valum)
			{	
				break;
			}
			else
				q=q->next;
		}
		if(q==NULL)
		{
			p->valum=0;
		}
		p=p->next;
	}	
}

void U_list(Linklist &la,Linklist &lb)
{
	Linklist p,q;
	p=la->next;
	while(p)
	{
		q=lb->next;
		while(q)
		{
			if(q->valum==p->valum)
			{	
				q->valum=0;
				p->valum=0;
				break;
			}
			else
				q=q->next;
		}
		p=p->next;
	
	}	
}

void D_list(Linklist &la,Linklist lb)
{
	Linklist p,q;
	p=la->next;
	while(p)
	{
		q=lb->next;
		while(q)
		{
			if(q->valum==p->valum)
			{	
				break;
			}
			else
				q=q->next;
		}
		if(q==NULL)
		{
			p->valum=0;
		}
		p=p->next;
	}	
}


int main()
{
	Linklist La,Lb;
	int n;
	scanf("%d",&len1);
	creat_list(La,len1);
	scanf("%d",&len2);
	creat_list(Lb,len2);
	QuickSort(La,NULL);
	Display(La);
	putchar('\n');
	QuickSort(Lb,NULL);
	Display(Lb);
	putchar('\n');
	//N_list(La,Lb);
	//Display(La);
	U_list(La,Lb);
	Display(La);
	Display(Lb);
	putchar('\n');
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
集合是一个数学概念,表示一组对象的整体,其中每个对象都是唯一的。集合可以进行、并、等操作,用于操作集合中的元素。在C语言中,可以使用链表来实现这些集合操作。 链表是一种数据结构,由若干个节点组成,每个节点都包括一个数据元素和指向下一个节点的指针。在C语言中,可以使用结构体来定义一个节点,如下所示: ``` struct Node { int data; struct Node* next; }; ``` 其中data表示节点存储的数据元素,next表示指向下一个节点的指针。利用这个结构体,可以定义一个链表链表的基本操作包括插入、删除和遍历。在集合、并、操作中,需要对两个链表进行遍历,并根据不同的操作进行元素的增删。 假设有两个链表list1和list2,分别存储两个集合。要实现集操作,可以遍历list1,对于其中的每个元素,判断其是否也在list2中出现,如果是,则将其添加到一个新的链表中。类似的,可以实现并集操作和集操作。 具体实现过程可以参考以下代码: ``` #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; // 判断链表中是否存在某个元素 int isExist(struct Node* head, int val) { struct Node* p = head; while (p != NULL) { if (p->data == val) { return 1; } p = p->next; } return 0; } // 求两个集合集 struct Node* intersect(struct Node* list1, struct Node* list2) { struct Node* head = NULL; // 链表的头结点 struct Node* tail = NULL; // 链表的尾结点 struct Node* p = list1; while (p != NULL) { if (isExist(list2, p->data)) { // 如果list2中也存在该元素,则添加到链表中 struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = p->data; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } p = p->next; } return head; } // 求两个集合的并集 struct Node* unionn(struct Node* list1, struct Node* list2) { struct Node* head = NULL; // 并集链表的头结点 struct Node* tail = NULL; // 并集链表的尾结点 struct Node* p = list1; while (p != NULL) { struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = p->data; node->next = NULL; if (!isExist(head, p->data)) { // 如果并集链表中不存在该元素,则添加到链表中 if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } p = p->next; } p = list2; while (p != NULL) { struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = p->data; node->next = NULL; if (!isExist(head, p->data)) { if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } p = p->next; } return head; } // 求两个集合集 struct Node* subtract(struct Node* list1, struct Node* list2) { struct Node* head = NULL; // 链表的头结点 struct Node* tail = NULL; // 链表的尾结点 struct Node* p = list1; while (p != NULL) { if (!isExist(list2, p->data)) { // 如果list2中不存在该元素,则添加到链表中 struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = p->data; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } p = p->next; } return head; } // 打印链表 void printList(struct Node* head) { printf("["); struct Node* p = head; while (p != NULL) { printf("%d", p->data); if (p->next != NULL) { printf(", "); } p = p->next; } printf("]\n"); } int main() { // 定义两个链表,list1={1,3,5,7,9},list2={2,4,6,8,10} struct Node* list1 = (struct Node*) malloc(sizeof(struct Node)); list1->data = 1; list1->next = (struct Node*) malloc(sizeof(struct Node)); list1->next->data = 3; list1->next->next = (struct Node*) malloc(sizeof(struct Node)); list1->next->next->data = 5; list1->next->next->next = (struct Node*) malloc(sizeof(struct Node)); list1->next->next->next->data = 7; list1->next->next->next->next = (struct Node*) malloc(sizeof(struct Node)); list1->next->next->next->next->data = 9; list1->next->next->next->next->next = NULL; struct Node* list2 = (struct Node*) malloc(sizeof(struct Node)); list2->data = 2; list2->next = (struct Node*) malloc(sizeof(struct Node)); list2->next->data = 4; list2->next->next = (struct Node*) malloc(sizeof(struct Node)); list2->next->next->data = 6; list2->next->next->next = (struct Node*) malloc(sizeof(struct Node)); list2->next->next->next->data = 8; list2->next->next->next->next = (struct Node*) malloc(sizeof(struct Node)); list2->next->next->next->next->data = 10; list2->next->next->next->next->next = NULL; // 求集 struct Node* list3 = intersect(list1, list2); printf("Intersect: "); printList(list3); // 求并集 struct Node* list4 = unionn(list1, list2); printf("Union: "); printList(list4); // 求集 struct Node* list5 = subtract(list1, list2); printf("Subtract: "); printList(list5); return 0; } ``` 运行结果如下: ``` Intersect: [ ] Union: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] Subtract: [1, 3, 5, 7, 9] ``` 说明集为空集,表示list1和list2没有共同的元素;并集包含了list1和list2的所有元素,集包含了list1中没有出现在list2中的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值