数据结构 求集合的并,交,差, 运行的结果不对,但是找不出错误,求帮助
#include <stdio.h>#include <malloc.h>
#include <stdlib.h>
typedef struct SqList
{
char ch;
struct SqList * next;
} SqList, * pSqList;
void mainmenu();
pSqList creatb();
void print(pSqList);
pSqList Union (pSqList, pSqList);
pSqList Insertsection (pSqList, pSqList);
pSqList Difference (pSqList, pSqList);
void Delrepeat(pSqList);
int main(void)
{
pSqList va_head, vb_head, vc_head;
va_head = NULL;
vb_head = NULL;
vc_head = NULL;
int a = 0;
int flag = 0;
while(a!=5)
{
mainmenu();
scanf("%d",&a);
switch(a)
{
case 1:
printf("请输入链表中各结点的数据,输入#表示输入结束:\n");
va_head = creatb();
printf("输入的第一个链表为:");
print(va_head);
printf("请输入链表中各结点的数据,输入#表示输入结束:\n");
vb_head = creatb();
printf("输入的第二个链表为:");
print(vb_head);
Delrepeat(va_head);
Delrepeat(vb_head);
flag = 1;
break;
case 2:
if(flag==1)
{
vc_head = Union(va_head, vb_head);
printf("完成并运算后的集合为:");
print(vc_head);
}
else
{
printf("请先执行1操作\n");
}
break;
case 3:
if(flag == 1)
{
vc_head = Insertsection(va_head,vb_head);
printf("完成交运算后的集合为:");
print(vc_head);
}
else
{
printf("请先执行1操作\n");
}
break;
case 4:
if(flag == 1)
{
vc_head = Difference(va_head,vb_head);
printf("完成差运算后的集合为:");
print(vc_head);
}
break;
case 5:
exit(0);
break;
default:
printf("输入错误!\n");
break;
}
}
return 0;
}
void mainmenu()
{
printf("\n\n");
printf("*******************欢迎进入链表操作系统,请选择操作***************\n");
printf("1.输入单链表(尾部插入)\n");
printf("2.运行集合的并运算\n");
printf("3.运行集合的交运算\n");
printf("4.运行集合的差运算\n");
printf("5.结束操作退出系统********************\n");
}
pSqList creatb()
{
pSqList head = (pSqList)malloc(sizeof(SqList));
pSqList p = (pSqList)malloc(sizeof(SqList));
pSqList tail;
head->next = NULL;
tail = head;
p->ch = getchar();
while (p->ch != '#')
{
tail->next = p;
tail = p;
p = (pSqList)malloc(sizeof(SqList));
p->ch=getchar();
}
tail->next = NULL;
return head;
}
void print(pSqList head)
{
pSqList p;
p = head->next;
if(head == NULL)
{
printf("该表为空表");
}
else
{
while(p!=NULL)
{
printf("%c ", p->ch);
p = p->next;
}
}
printf("\n");
}
void Delrepeat(pSqList head)
{
pSqList p, q;
p = head->next;
while(p!=NULL)
{
q = p->next;
while(q!=NULL && q->ch!=p->ch)
q = q->next;
if(q!=NULL)
{
p->next = q->next ;
free(q);
}
p=p->next ;
}
}
pSqList Union (pSqList head1, pSqList head2)
{
pSqList head = (pSqList)malloc(sizeof(SqList));
head->next = NULL;
pSqList tail, p;
tail = head;
pSqList p1 = head1->next;
pSqList p2 = head2->next;
while(p1!=NULL)
{
pSqList s = (pSqList)malloc(sizeof(SqList));
s->ch=p1->ch;
s->next = NULL;
tail->next = s;
tail = s;
p1 = p1->next;
}
while(p2!=NULL)
{
p = head->next;
while(p!=NULL && (p2->ch != p->ch))
p = p->next;
if(p==NULL)
{
pSqList s = (pSqList)malloc(sizeof(SqList));
s->ch = p2->ch;
s->next = NULL;
tail->next = s;
tail = s;
}
p2 = p2->next;
}
tail->next = NULL;
return head;
}
pSqList Insertsection (pSqList head1, pSqList head2)
{
pSqList head = (pSqList)malloc(sizeof(SqList));
head->next = NULL;
pSqList tail;
tail = head;
pSqList p1 = head1->next;
pSqList p2;
while(p1!=NULL)
{
p2 = head2->next;
while(p2!=NULL && (p1->ch != p2->ch))
p2 = p2->next;
if(p2 != NULL)
{
pSqList s = (pSqList)malloc(sizeof(SqList));
s->ch = p1->ch;
s->next = NULL;
tail->next = s;
tail = s;
}
p1 = p1->next;
}
tail->next = NULL;
return head;
}
pSqList Difference (pSqList head1, pSqList head2)
{
pSqList head = (pSqList)malloc(sizeof(SqList));
head->next = NULL;
pSqList p, q, tail;
tail = head;
pSqList p1 = head1->next;
pSqList p2;
while(p1!=NULL)
{
pSqList s = (pSqList)malloc(sizeof(SqList));
s->ch=p1->ch;
tail->next = s;
tail = s;
p1 = p1->next;
}
tail->next = NULL;
p = head->next;
q = head;
while(q->next!=NULL)
{
p2 = head2->next;
while(p2!=NULL && (p->ch != p2->ch))
p2 = p2->next;
if(p2!=NULL)
{
q->next = p->next;
free(p);
p = q->next;
}
else
{
q = p;
p = p->next ;
}
}
return head;
}
这个数据结构的问题,是求集合的并,交,差. 找不出其中的错误,求帮助