集合的运算
实现集合的交 并 补运算;
采用链表数据结构.
1 #include "stdafx.h" 2 #include<stdio.h> 3 #include<stdlib.h> 4 5 typedef struct LNode//定义结构体类型指针 6 { 7 char data; 8 struct LNode*next; 9 }*pointer; 10 11 void readdata(pointer head)//定义输入集合函数 12 { 13 pointer p; 14 char tmp; //tmp为输入的字符 15 scanf_s("%c", &tmp); 16 while (tmp != '\n') 17 { 18 p = (pointer)malloc(sizeof(struct LNode)); 19 p->data = tmp; 20 p->next = head->next; 21 head->next = p; 22 scanf_s("%c", &tmp); 23 } 24 } 25 void pop(pointer head)//定义输出集合函数 26 { 27 pointer p; 28 p = head->next; 29 while (p != NULL) 30 { 31 printf("%c", p->data); 32 p = p->next; 33 } 34 printf("\n"); 35 } 36 void and(pointer head1, pointer head2, pointer head3)//定义集合的并集函数 37 { 38 pointer p1, p2, p3; //p1 p2 p3为三个单链表用来存放集合1 2 3 39 p1 = head1->next; 40 while (p1 != NULL) 41 { 42 p3 = (pointer)malloc(sizeof(struct LNode)); 43 p3->data = p1->data; 44 p3->next = head3->next; 45 head3->next = p3; 46 p1 = p1->next; 47 } 48 p2 = head2->next; 49 while (p2 != NULL) 50 { 51 p1 = head1->next; 52 while ((p1 != NULL) && (p1->data != p2->data)) 53 p1 = p1->next; 54 if (p1 == NULL) 55 { 56 p3 = (pointer)malloc(sizeof(struct LNode)); 57 p3->data = p2->data; 58 p3->next = head3->next; 59 head3->next = p3; 60 } 61 p2 = p2->next; 62 } 63 } 64 65 void or(pointer head1, pointer head2, pointer head3)//定义集合的交集函数 66 { 67 pointer p1, p2, p3; //p1 p2 p3为三个单链表用来存放集合1 2 3 68 p1 = head1->next; 69 while (p1 != NULL) 70 { 71 p2 = head2->next; 72 while ((p2 != NULL) && (p2->data != p1->data)) 73 p2 = p2->next; 74 if ((p2 != NULL) && (p2->data == p1->data)) 75 { 76 p3 = (pointer)malloc(sizeof(struct LNode)); 77 p3->data = p1->data; 78 p3->next = head3->next; 79 head3->next = p3; 80 } 81 p1 = p1->next; 82 } 83 } 84 85 void differ(pointer head1, pointer head2, pointer head3)//定义集合的差集函数 86 { 87 pointer p1, p2, p3; //p1 p2 p3为三个单链表用来存放集合1 2 3 88 p1 = head1->next; 89 while (p1 != NULL) 90 { 91 p2 = head2->next; 92 while ((p2 != NULL) && (p2->data != p1->data)) 93 p2 = p2->next; 94 if (p2 == NULL) 95 { 96 p3 = (pointer)malloc(sizeof(struct LNode)); 97 p3->data = p1->data; 98 p3->next = head3->next; 99 head3->next = p3; 100 } 101 p1 = p1->next; 102 } 103 } 104 105 void main()//主函数 106 { 107 int x; 108 printf("(输入数据,按回车键结束,第一个集合大于第二个集合)\n"); 109 pointer head1, head2, head3; 110 head1 = (pointer)malloc(sizeof(struct LNode)); 111 head1->next = NULL; 112 head2 = (pointer)malloc(sizeof(struct LNode)); 113 head2->next = NULL; 114 head3 = (pointer)malloc(sizeof(struct LNode)); 115 head3->next = NULL; 116 printf("请输入集合1(小写字母):\n"); 117 readdata(head1);//调用输入集合函数 118 printf("请输入集合2(小写字母):\n"); 119 readdata(head2);//调用输入集合函数 120 B : printf("1.并集 2.交集 3.差集 4.结束 x.重新运算\n"); 121 do{ 122 printf("请选择序号\n"); 123 scanf_s("%d", &x,1); 124 switch (x) 125 { 126 case 1: 127 printf("两集合的并是\n"); 128 and(head1, head2, head3);//调用并集函数 129 pop(head3); 130 head3->next = NULL; 131 break; 132 case 2: 133 printf("两集合的交是\n"); 134 or(head1, head2, head3);//调用交集函数 135 pop(head3); 136 head3->next = NULL; 137 break; 138 case 3: 139 printf("两集合的差是\n"); 140 differ(head1, head2, head3);//调用差集函数 141 pop(head3); 142 head3->next = NULL; 143 break; 144 case 4: 145 break; 146 default: 147 goto B; 148 149 } 150 }while(x != 4); 151 }