交并补集链表实现

#include<iostream>
using namespace std;
struct node {
	int num;
	node* next;
};
node* creat() {
	node* head = new node;
	head->next = nullptr;
	return head;
}
void fuzhi(node* head, int n) {
	node* h = head;
	for (int i = 0; i < n; i++)
	{
		node* nomal = new node;
		int number;
		cin >> number;
		nomal->num = number;
		nomal->next = nullptr;
		h->next = nomal;
		h = nomal;
	}
}
bool search(node* head, int target) {
	bool m = false;
	for (node* p = head->next; p != nullptr; p = p->next) {
		if (p->num == target) {
			m = true;
			break;
		}
	}
	return m;
}
void show(node * head){
	for (node* h1 = head->next; h1 != nullptr; h1 = h1->next) {
		cout << h1->num << " ";
	 }	
}
void insert(node* head,int target) {
	if (search(head, target) == true) {
		cout << "已经有这个元素了" << endl;
		return;
	}
	else {
		node* h2 = new node;
		h2->num = target;
		h2->next = head->next;
		head->next = h2;
	}
}
bool Isziji(node* h1, node* h2) {
	bool m = true;
	for (node* p = h1->next; p != nullptr; p = p->next) {
		if (search(h2, p->num) == false) {
			m = false;
			break;
		}
	}
	return m;
}
bool empty(node*head){
	bool m = true;
	node* p = head->next;
	if (p != nullptr) {
		m = false;
	}
	return m;

}
int nodesize(node* head) {
	int a = 0;
	if (empty(head) == true)
	{
		cout << "是空集" << endl;
		return 0;
	}
	else
	{
		for (node* p = head->next; p != nullptr;p=p->next) {
			a++;
		}
	}
	return a;
}
bool equalto(node* h1, node* h2) {
	     bool m = true;
	if (nodesize(h1) != nodesize(h2)) {
		return 0;
	}
	        
	else {
		for (node* p = h1->next; p != nullptr; p = p->next) {
			if (search(h2, p->num) == false) {
				m = false;
				break;
			}
		}
	}
	          return m;
}
void jiaoji(node* h1, node* h2) {
	node* cun = creat();
	for (node* p = h1->next; p != nullptr; p = p->next) {
		if (search(h2, p->num) == true) {
			insert(cun, p->num);
		}
	}
	show(cun);

}
void bingji(node* h1, node* h2) {
	for (node* p = h2->next; p != nullptr; p = p->next) {
		if (search(h1, p->num) == false) {
			insert(h1, p->num);
		}
	}
	show(h1);
}
void caji(node* h1, node* h2) {
	node* cun = creat();
	for (node* p = h1->next; p != nullptr; p = p->next) {
		if (search(h2, p->num) == false) {
			insert(cun, p->num);
		}
	}
	show(cun);
}
node* moveto(node* head, int pos) {
	node* p = head;
	for (int i = 0; i < pos;i++){
		p = p->next;
	}
	return p;
}
void delet(node* h1, int pos) {
	node* p = moveto(h1, pos - 1);
	node* q = p->next;
	p->next = p->next->next;
	delete[]q;
}
int main() {
	node* head1 = creat();
	cout << "请输入5个链表元素:" << endl;
	int n = 5;
	fuzhi(head1, n);
	show(head1);
	/*int target;
	cout << "请输入待查找数:" << endl;
	cin >> target;
	if (search(head, target) == true)
		cout << "找到了" << endl;
	else
		cout << "不存在这个数" << endl;*/
	  /*  int tar;
	cout << "请输入要插入的元素:" << endl;
	    cin >> tar;
		insert(head, tar);
		show(head);*/
	node* head2 = creat();
	cout << endl;
	cout << "请输入5个链表元素:" << endl;
	int n1 = 5;
	fuzhi(head2, n1);
	show(head2);
	/*if (Isziji(head2, head1) == true) {
		cout << "head2是head1的子集" << endl;
	}
	else {
		cout << "head2不是head1的子集" << endl;
	}*/
	/*if (equalto(head1, head2) == true)
		cout << "两个集合相等" << endl;
	else
		cout << "两个集合不相等" << endl;*/
	/*cout << endl;
	cout << "交集是:" << endl;
	jiaoji(head1, head2);*/
	/*cout << endl;
	cout << "并集是:" << endl;
	bingji(head1, head2);*/
	cout << endl;
	cout << "差集是:" << endl;
	caji(head1, head2); 
	delet(head1, 3);
	cout << "删除后的集合为:" << endl;
	show(head1);
	cout << moveto(head1, 3);
	system("PAUSE");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值