数据结构课程设计 #include using namespace std; #include<stdlib.h> typedef struct LNode//结构体 { char data; struct LNode *next; }*linklist; void in_put(linklist head)//输入 { linklist p; char tmp=cin.get(); cin>>tmp; while(tmp!=’\n’) { p=(linklist)malloc(sizeof(struct LNode)); p->data=tmp; p->next=head->next; head->next=p; tmp=cin.get(); } } void out_put(linklist head)//输出 { linklist p; p=head->next; while(p!=NULL) { cout<data; p=p->next; } cout<<"\n"; } void bingji(linklist head1,linklist head2,linklist head3)//集合的并集 { linklist p1; linklist p2; linklist p3; p1=head1->next; while(p1!=NULL) { p3=(linklist)malloc(sizeof(struct LNode)); p3->data=p1->data; p3->next=head3->next; head3->next=p3; p1=p1->next; } p2=head2->next; while(p2!=NULL) { p1=head1->next; while((p1!=NULL)&&(p1->data!=p2->data)) p1=p1->next; if (p1NULL) { p3=(linklist)malloc(sizeof(struct LNode)); p3->data=p2->data; p3->next=head3->next; head3->next=p3; } p2=p2->next; } } void jiaoji(linklist head1,linklist head2,linklist head3)//集合的交集 { linklist p1,p2,p3; p1=head1->next; while(p1!=NULL) { p2=head2->next; while((p2!=NULL)&&(p2->data!=p1->data)) p2=p2->next; if((p2!=NULL)&&(p2->datap1->data)) { p3=(linklist)malloc(sizeof(struct LNode)); p3->data=p1->data; p3->next=head3->next; head3->next=p3; } p1=p1->next; } } void chaji(linklist head1,linklist head2,linklist head3)//集合的差集 { linklist p1,p2,p3; p1=head1->next; while(p1!=NULL) { p2=head2->next; while((p2!=NULL)&&(p2->data!=p1->data)) p2=p2->next; if(p2==NULL) { p3=(linklist)malloc(sizeof(struct LNode)); p3->data=p1->data; p3->next=head3->next; head3->next=p3; } p1=p1->next; } } int main()//主函数 { int x; linklist head1,head2,head3; head1=(linklist)malloc(sizeof(struct LNode)); head1->next=NULL; head2=(linklist)malloc(sizeof(struct LNode)); head2->next=NULL; head3=(linklist)malloc(sizeof(struct LNode)); head3->next=NULL; cout<<“请输入集合1:”; in_put(head1); cout<<“请输入集合2:”; in_put(head2); while(1) { cout<<“1.并集\n”; cout<<“2.交集\n”; cout<<“3.差集\n”; cout<<“4.退出程序\n”; cin>>x; switch(x) { case 1: cout<<“两集合的并是”; bingji(head1,head2,head3);//并集 out_put(head3); head3->next=NULL; break; case 2: cout<<“两集合的交是”; jiaoji(head1,head2,head3);//交集 out_put(head3); head3->next=NULL; break; case 3: cout<<“两集合的差是”; chaji(head1,head2,head3);//差集 out_put(head3); head3->next=NULL; break; case 4: break; default: cout<<“选择错误,请重新输入\n”; } } return 0; }