C语言 求两个单链表的交与并



程序生成两个链表的并集和交集。但是,它没有生成预期的输出,其中不应该出现列表1和列表2中的重复值,甚至没有显示交集列表。如何解决这个问题?

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
} node;
void insertAtBeg(node **head, int ele){
node *newnode = (node*)malloc(sizeof(node));
newnode->data = ele;
newnode->next = (*head);
(*head) = newnode;
}
int isPresent(node *temp, int ele){
node *t = temp;
while(t != NULL){
if(t->data == ele)
return 1;
t = t->next;
}
return 0;
}
void printList(node *n){
while(n != NULL){
printf("%d->",n->data);
n = n->next;
}
}
node* getUnion(node *head1,node *head2){
node *result = NULL;
node *t1 = head1;
node *t2 = head2;
while(t1 != NULL){
insertAtBeg(&result, t1->data);
t1 = t1->next;
}
while(t2!=NULL){
if(!isPresent(result, t2->data));
insertAtBeg(&result,t2->data);
t2 = t2->next;
}
return result;
}
node *getIntersection(node *head1,node *head2){
node* result = NULL;
node* t1 = head1;
while(t1 != NULL){
if(isPresent(head2, t1->data))
insertAtBeg(&result,t1->data);
t1 = t1->next;
}
return result;
}
int main(){
node *intersection = NULL;
node *unin = NULL;
node *List1;
node *List2;
List1 = List2 = NULL;
int i,n,m,temp;

printf("Enter the size of the first linked list:n");
scanf("%d",&n);
printf("Enter %d elementsn",n);
for(i = 0;i < n;i++){
scanf("%d",&temp);
insertAtBeg(&List1,temp);
}
printf("Displaying list 1:n");
printList(List1);
printf("nEnter the size of the second linked list:n");
scanf("%d",&m);
printf("Enter %d elementsn",m);
for(i = 0;i < m;i++){
scanf("%d",&temp);
insertAtBeg(&List2,temp);
}
printf("Displaying list 2:n");
printList(List2);
unin = getUnion(List1,List2);
intersection = getIntersection(List1,List2);
printf("nLinked List with Union of List1 and List2:n");
printList(unin);
printf("nLinked List with Intersection of List1 and List2:n");
printList(intersection);
return 0;
}

生成的输出:输入第一个链表的大小:4输入4个元素3 4 6 1显示列表1:1→6→4→3→输入第二个链表的大小:4输入4个元素8 9 10 1显示列表2:8 9 10 1→→→→List1和List2并的链表:10 8→9→→1→3→4→6→1→List1与List2相交的链表:1→

在函数getUnion中有一个错别字

while(t2!=NULL){
if(!isPresent(result, t2->data));
^^^^
insertAtBeg(&result,t2->data);
t2 = t2->next;
}

去掉分号

在调用

之后
printList(intersection);  

输出新的行字符,例如

putchar( 'n' );

请注意,如果列表没有在函数中更改,则应声明带有const限定符的指针的参数,例如

int isPresent( const node *temp, int ele){
const node *t = temp;
while(t != NULL){
if(t->data == ele)
return 1;
t = t->next;
}
return 0;
}
void printList(const node *n){
while(n != NULL){
printf("%d->",n->data);
n = n->next;
}
}
node* getUnion( const node *head1,const node *head2){
node *result = NULL;
const node *t1 = head1;
const node *t2 = head2;
while(t1 != NULL){
insertAtBeg(&result, t1->data);
t1 = t1->next;
}
while(t2!=NULL){
if(!isPresent(result, t2->data))
insertAtBeg(&result,t2->data);
t2 = t2->next;
}
return result;
}
node *getIntersection(const node *head1,const node *head2){
node* result = NULL;
const node* t1 = head1;
while(t1 != NULL){
if(isPresent(head2, t1->data))
insertAtBeg(&result,t1->data);
t1 = t1->next;
}
return result;
}

相关内容

  • 没有找到相关文章

最新更新