Rewrite requirements 1: Rewrite as a single-linked list structure can be set for any length of integer set
#include <cstdlib>#include<iostream>using namespacestd;structlinknode{intdata; Linknode*Next; };classset{ Public: structlinknode* creat (intX[],intLen); structlinknode* Copy (linknode*aHead); intNotin (intelem,linknode*chead) {Linknode*p; P=Chead; while(p) {if(elem==p->data)return 0; P=p->Next; } return 1; } voidInsert (linknode* bhead,linknode*chead); voidOutput (linknode*chead) {Linknode* p=chead->Next; while(p) {cout<<p->data<<"\ t"; P=p->Next; } cout<<Endl; }};structlinknode* Set::creat (intX[],intLen) {Linknode* phead=NewLinknode; Phead->next=NULL; Linknode*p; P=Phead; for(intI=0; i<len;i++) {Linknode* newlinknode=NewLinknode; Newlinknode->next=NULL; Newlinknode->data=X[i]; P->next=Newlinknode; P=Newlinknode; } returnPhead;}structlinknode* Set::copy (linknode*aHead) {Linknode* chead=NewLinknode; Chead->next=NULL; Linknode* p,*R; P=ahead->Next; R=Chead; while(p) {Linknode* newlinknode=NewLinknode; Newlinknode->next=NULL; Newlinknode->data=p->data; P=p->Next; R->next=Newlinknode; R=Newlinknode; } returnChead;}voidSet::insert (Linknode *bhead,linknode*chead) {Linknode* q,*s,*T; Q=bhead->Next; S=chead->Next; while(s) {T=s; S=s->Next; } while(q) {if(Notin (q->data,chead)! =0) {Linknode* newlinknode=NewLinknode; Newlinknode->next=NULL; Newlinknode->data=q->data; T->next=Newlinknode; T=Newlinknode; } q=q->Next; } }intMainintargcChar*argv[]) { ints1[]={1,3,5,7,9}; ints2[]={1,2,3,4,5,6}; Linknode* ahead,*bhead,*chead,*Head; SETSet; AHead=Set. creat (S1,sizeof(S1)/sizeof(s1[0])); Bhead=Set. creat (S2,sizeof(S2)/sizeof(s2[0])); Chead=Set. Copy (AHead); Set. Insert (Bhead,chead); Set. Output (Chead); System ("PAUSE"); returnexit_success;}
Rewrite requirement 2: intersection of two integer sets of arbitrary length
#include <cstdlib>#include<iostream>using namespacestd;structlinknode{intdata; Linknode*Next; };classset{ Public: structlinknode* creat (intX[],intLen); structlinknode* Copy (linknode*aHead); intNotin (intelem,linknode*chead) {Linknode*p; P=Chead; while(p) {if(elem==p->data)return 1; P=p->Next; } return 0; } structlinknode* Insert (linknode* bhead,linknode*chead); voidOutput (linknode*chead) {Linknode* p=chead->Next; while(p) {cout<<p->data<<"\ t"; P=p->Next; } cout<<Endl; }};structlinknode* Set::creat (intX[],intLen) {Linknode* phead=NewLinknode; Phead->next=NULL; Linknode*p; P=Phead; for(intI=0; i<len;i++) {Linknode* newlinknode=NewLinknode; Newlinknode->next=NULL; Newlinknode->data=X[i]; P->next=Newlinknode; P=Newlinknode; } returnPhead;}structlinknode* Set::copy (linknode*aHead) {Linknode* chead=NewLinknode; Chead->next=NULL; Linknode* p,*R; P=ahead->Next; R=Chead; while(p) {Linknode* newlinknode=NewLinknode; Newlinknode->next=NULL; Newlinknode->data=p->data; P=p->Next; R->next=Newlinknode; R=Newlinknode; } returnChead;}structlinknode* Set::insert (Linknode *bhead,linknode*chead) {Linknode* q,*T; Linknode* head=NewLinknode; Q=bhead->Next; Head->next=NULL; T=Head; while(q) {if(Notin (q->data,chead)! =0) {Linknode* newlinknode=NewLinknode; Newlinknode->next=NULL; Newlinknode->data=q->data; T->next=Newlinknode; T=Newlinknode; } q=q->Next; } returnHead;}intMainintargcChar*argv[]) { ints1[]={1,3,5,7,9}; ints2[]={1,2,3,4,5,6}; Linknode* ahead,*bhead,*chead,*Head; SETSet; AHead=Set. creat (S1,sizeof(S1)/sizeof(s1[0])); Bhead=Set. creat (S2,sizeof(S2)/sizeof(s2[0])); Chead=Set. Copy (AHead); Head=Set. Insert (Bhead,chead); Set. Output (Head); System ("PAUSE"); returnexit_success;}
C + + Programming practice Guidance 1.5 for two integer set merge set rewrite requirements implementation