设计要求是单链表处理交集并集,能自动对输入的重复数据进行处理。我是纯新手,求大神帮忙修改代码,希望能详细注释说明,最好帮我简化一下代码,谢谢!!!
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
char data;
Node* next;
}SeqList;
bool IsContain(SeqList List, int x);
bool IsContain1(SeqList* List, char x);
SeqList* Intersection(SeqList* List1, SeqList* List2);
SeqList* Union(SeqList* List1, SeqList* List2);
SeqList* CreateSeqList(char n);
void Print(SeqList* List);
//判断一个元素是否包含于一个集合
bool IsContain(SeqList* List, char x)
{
SeqList* p = List->next;
while (p)
{
if (p->data == x)
return true;
p = p->next;
}
return false;
}
//用于判断一个元素是否包含于一个不用头结点表示的集合
bool IsContain1(SeqList* List, char x)
{!
SeqList* p = List;
while (p != NULL)
{
if (p->data == x)
return true;
p = p->next;
}
return false;
}
//求交集
SeqList* Intersection(SeqList* List1, SeqList* List2)
{
SeqList* Seq, *Node, *k;
SeqList* p = List1->next;
Seq = (SeqList*)malloc(sizeof(SeqList));
k = Seq;
while (p != NULL)
{
if (IsContain(List2, p->data))
{
Node = (SeqList*)malloc(sizeof(SeqList));
Node->data = p->data;
k->next = Node;
k = k->next;
}
p = p->next; /*向后遍历*/
}
return Seq;
}
//求并集
SeqList* Union(SeqList* List1, SeqList* List2)
{
SeqList* Seq = (SeqList*)malloc(sizeof(SeqList));
SeqList* p = List1->next;
SeqList* q = List2->next;
SeqList* k = Seq;
while (p != NULL)
{
SeqList* node = (SeqList*)malloc(sizeof(SeqList));
node->data = p->data;
k->next = node;
k = k->next;
p = p->next;
}
while (q != NULL)
{
if (!IsContain(List1, q->data))
{
SeqList* node = (SeqList*)malloc(sizeof(SeqList));
node->data = q->data;
k->next = node;
k = k->next;
}
q = q->next;
}
return Seq;
}
//创建一个集合
SeqList* CreateSeqList(char n)
{
SeqList* Seq = (SeqList*)malloc(sizeof(SeqList));
SeqList* Head = (SeqList*)malloc(sizeof(SeqList));
Seq->data = '\n';
Seq->next = NULL;
// SeqList* p,*q;
//p = Seq;
//q = Seq;
printf("输入集合%c的元素:\n", n);
char a;
do
{
scanf("%c", &a);
} while (!((a >= 'a'&& a <= 'z') || (a >= '0'&&a <= '9') || (a >= 'A'&&a <= 'Z')) && a != '\n');
while (a != '\n')
{
// q = Seq;
if (IsContain1(Seq, a))
{
do{
scanf("%c", &a);
} while (!((a >= 'a'&&a <= 'z') || (a >= '0'&&a <= '9') || (a >= 'A'&&a <= 'Z')) && a != '\n');
}
else
{
SeqList* node = (SeqList*)malloc(sizeof(SeqList));
node->data = a;
node->next = Seq;
Seq = node;
do{
scanf("%c", &a);
} while (!((a >= 'a'&&a <= 'z') || (a >= '0'&&a <= '9') || (a >= 'A'&&a <= 'Z')) && a != '\n');
}
}
Head->next = Seq;
return Head;
}
//输出集合
void Print(SeqList* List)
{
SeqList* p = List->next;
printf("(");
while (p->next != NULL)
{
printf("%c", p->data);
p = p->next;
}
p = p->next;
printf("%c", p->data);
printf(")");
printf("\n");
}
int main()
{
SeqList*sq1, *sq2;
char cname1, cname2;
printf("输入第一个集合的名字:\n");
scanf("%c", &cname1);
getchar();
sq1 = CreateSeqList(cname1);
getchar();
printf("输入第二个集合的名字:\n");
scanf("%c", &cname2);
getchar();
sq2 = CreateSeqList(cname2);
printf("集合%c:\n", cname1);
Print(sq1);
printf("集合%c:\n", cname2);
Print(sq2);
printf("交集\n");
Print(Intersection(sq1, sq2));
printf("并集\n");
Print(Union(sq1, sq2));
return 0;
}