#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct Node Node;
typedef struct Node* PtrtoNode;
typedef PtrtoNode List;
struct Node{
int s;
List next;
};
void insert(List l,int t) ;
void menu(char s[][100]);
void outputC(List C);
void freeL(List L);
int main(){
List A=(Node*)malloc(sizeof(Node));
List B=(Node*)malloc(sizeof(Node));
A->next=NULL;
B->next=NULL;
char s[20][100]={
{"1---输入集合A和B"},
{"2---求集合A并B"
},
{"3---求集合A交B"},
{"4---求集合A-B"},
{"退出,输入一个负数!"
}
};
int i;
do{
menu(s);
cin>>i;
List C=(List)malloc(sizeof(Node));
C->next=NULL;
List p2=A;
List p3=B;
switch(i){
case 1:
system("cls");
cout<<"输入两个集合"<<endl;
char c;
do{
c=cin.get();
}while(c!='{');
while(1){
c=cin.peek();
if(c==' '){
cin.get();
continue;
}
if(c=='}'){
cin.get();
break;}
int n=0;
do{
n=10*n+c-'0';
cin.get();
c=cin.peek();
} while(isdigit(c));
insert(A,n);
}
do{
c=cin.get();
}while(c!='{');
while(1){
c=cin.peek();
if(c==' '){
cin.get();
continue;
}
if(c=='}'){
cin.get();
break;
}
int n=0;
do{
n=10*n+c-'0';
cin.get();
c=cin.peek();
} while(isdigit(c));
insert(B,n);
}
cout<<"compeleted"<<endl;
break;
case 2:
p2=p2->next;
p3=p3->next;
while(p2){
insert(C,p2->s);
p2=p2->next;
}
while(p3){
insert(C,p3->s);
p3=p3->next;
}
outputC(C);
break;
case 3:
p2=p2->next;
p3=p3->next;
while(p2){
p3=B->next;
while(p3){
if(p2->s==p3->s)
insert(C,p3->s);
p3=p3->next;
}
p2=p2->next;
}
outputC(C);
break;
case 4:
p2=p2->next;
p3=p3->next;
while(p2){
bool same=false;
p3=B->next;
while(p3){
if(p2->s==p3->s){
same=true;
}
p3=p3->next;
}
if(!same)
insert(C,p2->s);
p2=p2->next;
}
outputC(C);
break;
default:
cout<<" "<<i<<" ? "<<endl;
system("pause");
return 0;
}
freeL(C);
system("pause");
system("cls");
}while(i>=0);
return 0;
}
List findNode(List l,int cnt){
if(l->next)
return 0;
List p=l;
for(int i=0;i<=cnt;i++){
p=p->next;
if(p){
cout<<"不存在"<<endl;
return 0;
}
}
return p;
}
void menu(char s[][100]){
for(int j=0;j<20;j++){
cout<<s[j]<<endl;
}
}
void outputC(List C){
C=C->next;
while(C){
cout<<C->s<<' ';
C=C->next;
}
cout<<endl;
}
void insert(List l,int t){
while(l->next){
l=l->next;
if(l->s==t)
return;
}
List p=(List)malloc(sizeof(Node));
p->s=t;
p->next=NULL;
l->next=p;
}
void freeL(List L){
List p=L;
List q;
while(q){
q=p->next;
free(p);
p=q;
}
}
04-01
09-23
2934