通过键盘,分别输入集合A和B中的数据元素,要求数据元素类型为整数类型,输出两个集合的交、并、差。

            #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;
                }
            }
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值