C语言链表求并集和差集的问题,请大神帮我指出一下代码出错在哪里。
输入输出都是递增的正整数列,用链表实现求A-B的差集C1和并集C2。我用Cfree和VC6调试过了,报错都在unionset函数中最后第四个语句:p2->next,实在不...
输入输出都是递增的正整数列,用链表实现求A-B的差集C1和并集C2。
我用C free和VC6调试过了,报错都在unionset函数中最后第四个语句:p2->next,实在不知道为什么错了。在求并集的时候,我的算法类似合并两个有序线性表的算法。用两个在A,B上移动的指针完成的。
部分代码如下:(由于字数限制,只能暂时给出这么main和unionset函数,请大神回答时hi我,我把整个代码传过去)
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(Node)
typedef struct Node_List{
int num;
struct Node_List *next;
}Node;
int main()
{
//declare function:
Node *createList(char);
Node *differentSet(Node*,Node*);
Node *unionSet(Node*,Node*);
void isSortList(Node*);
void printList(Node*,char*);
void freeMemery(Node*);
Node *A=createList('A');
Node *B=createList('B');
//Check if the user input the data in ascending order
isSortList(A);
isSortList(B);
Node *C1=differentSet(A,B);
Node *C2=unionSet(A,B);
printList(C1,"C1");
printList(C2,"C2");
//release allocted space of heaps
freeMemery(A);A=NULL;
freeMemery(B);B=NULL;
freeMemery(C1);C1=NULL;
freeMemery(C2);C2=NULL;
return 0;
}
Node *unionSet(Node *i,Node *j)
{
Node *p1,*p2,*head=NULL;
//if empty lists exist.
if(i==NULL) return j;
if(j==NULL) return i;
if(p1=(Node *)malloc(LEN)) ;
else
{
printf("error!");
exit(0);
}
head=p1;
while(i&&j)
//neither the pointers moves to the end of the list
{
if(i->num < j->num)
{
//put A's element into C2
p1->num = i->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
i=i->next;
}
else if(j->num < i->num)
{
//put B's elment into C2
p1->num = i->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
//move to the next element of List B
j=j->next;
}
else if(i->num = j->num)
{
//put A's elment into C2
p1->num = i->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
//move to the next element of both List A and B
i=i->next;
j=j->next;
}
}
while(i)
//while the pointer pointing at A list hasn't moved to the end
{
p1->num = i->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
i=i->next;
}
while(j)
//while the pointer pointing at B list hasn't moved to the end
{
p1->num = j->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
j=j->next;
}
p2->next=NULL;
//release unuse space
free(p1);
p1=NULL;
return head;
} 展开
我用C free和VC6调试过了,报错都在unionset函数中最后第四个语句:p2->next,实在不知道为什么错了。在求并集的时候,我的算法类似合并两个有序线性表的算法。用两个在A,B上移动的指针完成的。
部分代码如下:(由于字数限制,只能暂时给出这么main和unionset函数,请大神回答时hi我,我把整个代码传过去)
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(Node)
typedef struct Node_List{
int num;
struct Node_List *next;
}Node;
int main()
{
//declare function:
Node *createList(char);
Node *differentSet(Node*,Node*);
Node *unionSet(Node*,Node*);
void isSortList(Node*);
void printList(Node*,char*);
void freeMemery(Node*);
Node *A=createList('A');
Node *B=createList('B');
//Check if the user input the data in ascending order
isSortList(A);
isSortList(B);
Node *C1=differentSet(A,B);
Node *C2=unionSet(A,B);
printList(C1,"C1");
printList(C2,"C2");
//release allocted space of heaps
freeMemery(A);A=NULL;
freeMemery(B);B=NULL;
freeMemery(C1);C1=NULL;
freeMemery(C2);C2=NULL;
return 0;
}
Node *unionSet(Node *i,Node *j)
{
Node *p1,*p2,*head=NULL;
//if empty lists exist.
if(i==NULL) return j;
if(j==NULL) return i;
if(p1=(Node *)malloc(LEN)) ;
else
{
printf("error!");
exit(0);
}
head=p1;
while(i&&j)
//neither the pointers moves to the end of the list
{
if(i->num < j->num)
{
//put A's element into C2
p1->num = i->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
i=i->next;
}
else if(j->num < i->num)
{
//put B's elment into C2
p1->num = i->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
//move to the next element of List B
j=j->next;
}
else if(i->num = j->num)
{
//put A's elment into C2
p1->num = i->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
//move to the next element of both List A and B
i=i->next;
j=j->next;
}
}
while(i)
//while the pointer pointing at A list hasn't moved to the end
{
p1->num = i->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
i=i->next;
}
while(j)
//while the pointer pointing at B list hasn't moved to the end
{
p1->num = j->num;
p2=p1;
p1=(Node *)malloc(LEN);
p2=p1->next;
j=j->next;
}
p2->next=NULL;
//release unuse space
free(p1);
p1=NULL;
return head;
} 展开
1个回答
展开全部
在进行交集、并集运算前,必须确保两个集合是有序的,且各个集合的元素必须是唯一的。
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(Node)
typedef struct Node_List {
int num;
struct Node_List *next;
}Node;
int main() {
Node *createList(char);
Node *differentSet(Node*,Node*);
Node *unionSet(Node*,Node*);
void isSortList(Node*);
void printList(Node*,char*);
void freeMemery(Node*);
Node *A = createList('A');
Node *B = createList('B');
isSortList(A);
isSortList(B);
Node *C1 = differentSet(A,B);
Node *C2 = unionSet(A,B);
printList(C1,"C1");
printList(C2,"C2");
freeMemery(A);A = NULL;
freeMemery(B);B = NULL;
freeMemery(C1);C1 = NULL;
freeMemery(C2);C2 = NULL;
return 0;
}
Node *unionSet(Node *i,Node *j) {
Node *p1,*p2,*head = NULL;
if(i == NULL) return j;
if(j == NULL) return i;
if(p1 = (Node *)malloc(LEN)) ;
else {
printf("error!");
exit(0);
}
head = p1;
while(i && j) {
if(i->num < j->num) {
p1->num = i->num;
p2 = p1;
p1 = (Node *)malloc(LEN);
p2 = p1->next;
i=i->next;
}
else if(j->num < i->num) {
p1->num = i->num;
p2 = p1;
p1 = (Node *)malloc(LEN);
p2 = p1->next;
j = j->next;
}
else if(i->num == j->num) { // 是==,不是=
p1->num = i->num;
p2 = p1;
p1 = (Node *)malloc(LEN);
p2 = p1->next;
i = i->next;
j = j->next;
}
}
while(i) {
p1->num = i->num;
p2 = p1;
p1 = (Node *)malloc(LEN);
p2 = p1->next;
i = i->next;
}
while(j) {
p1->num = j->num;
p2 = p1;
p1 = (Node *)malloc(LEN);
p2 = p1->next;
j = j->next;
}
p2->next = NULL;
free(p1);
p1 = NULL;
return head;
}
更多追问追答
追问
请告诉我在哪一行做了修改呢??
追答
为了“显眼”,只有一行有注释,你仔细找找。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询