#include <stdio.h>#include <stdlib.h>typedef struct Node{ int data;//定义元素类型为整型 struct Node *next;}LNode,*LinkList;//建立链表结构体int length_C=0; //定义c初始长度为零 void InitLNode(LNode* pHeader,int len){//初始化链表 int i; LNode *pNewNode; LNode *pCurNode; pHeader->next=pNewNode; pCurNode = pHeader; for(i=0;i<len;i++) { pNewNode = (LNode*)malloc(sizeof(LNode)); scanf("%d",&pNewNode->data); pNewNode->next = NULL; pCurNode->next = pNewNode; pCurNode = pNewNode; }} LNode* InterSet(LNode *p1,LNode *p2,LNode *C){ LNode *pCurNode1 = p1; LNode *pCurNode2 = p2; LNode *pCurNode3 = C; LNode *pNewNode3; C->next = pNewNode3; while(pCurNode1->next!=NULL) { pCurNode1=pCurNode1->next; while(pCurNode2->next!=NULL) { pCurNode2=pCurNode2->next; if(pCurNode1->data == pCurNode2->data) { pNewNode3 = (LNode*)malloc(sizeof(LNode)); length_C++; pNewNode3->data = pCurNode1->data; pNewNode3->next = NULL; pCurNode3->next = pNewNode3; pCurNode3=pNewNode3; } } pCurNode2 = p2; } pCurNode3 = C; return C;}void Display(LNode *pHeader){ if(length_C!=0) { LNode *pCurNode; pCurNode=pHeader; while(pCurNode->next!=NULL) { pCurNode = pCurNode->next; printf("%d ",pCurNode->data); } } }int main(){ LNode *A; LNode *B; LNode *C; int length_A,length_B ; //定义AB的长度 A = (LNode*)malloc(sizeof(LNode)); B = (LNode*)malloc(sizeof(LNode)); C = (LNode*)malloc(sizeof(LNode)); //开辟三个存储空间 printf("请输入A的长度及A的元素"); scanf("%d",&length_A); InitLNode(A,length_A); printf("请输入B的长度及B的元素"); scanf("%d",&length_B); InitLNode(B,length_B); InterSet(A,B,C);//进行交集运算,存储到链表C Display(C); return 0;}
目前暂无任何回答
- 0 回答
- 0 关注
- 851 浏览
添加回答
举报
0/150
提交
取消