#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;
}
交并补集链表实现
最新推荐文章于 2024-07-25 22:14:03 发布