早教吧 育儿知识 作业答案 考试题库 百科 知识分享

《数据结构》求集合{1.3.5.8.9}和集合{2.3.6.8.9.15}的交集,并输出结果

题目详情
《数据结构》求集合{1.3.5.8.9}和集合{2.3.6.8.9.15}的交集,并输出结果
▼优质解答
答案和解析
#include #include struct node { int data; struct node *next; }; typedef struct node *pointer,*lklist; //--------建表------- lklist initiate(lklist head) { head=(pointer)malloc(sizeof(struct node)); head->next=0; return head; } //-------输入表,以32767结尾---------- lklist setup(lklist head) { int x; pointer p=(pointer)malloc(sizeof(struct node)),q; p=head; scanf("%d",&x); while(x!=32767) { q=(pointer)malloc(sizeof(struct node)); q->data=x; p->next=q; p=q; scanf("%d",&x); } p->next=0; return head; } //-------求交集并输出------------ void disqual(lklist la1,lklist la2) { pointer p,q,s; q=la2->next; while(q) { p=la1; while(p->next) { if(p->next->data!=q->data) p=p->next; else { printf("%d ",p->next->data); p=p->next; } } q=q->next; } } void main() { pointer la1=(pointer)malloc(sizeof(struct node)), la2=(pointer)malloc(sizeof(struct node)); printf("请输入la(以32767结尾):\n"); la1=initiate(la1); la1=setup(la1); printf("请输入lb(以32767结尾):\n"); la2=initiate(la2); la2=setup(la2); printf("交集为: "); disqual(la1,la2); printf("\n"); } 学数据结构的时候习惯了用32767作结束表输入,你也可以改成别的你想要的值。