該程式生成 2 個鏈表的并集和交集。但是,它不會生成預期的輸出,其中不應存在串列 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 elements\n",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 elements\n",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:1->10->9->8-> List1 和 List2 并集的鏈表:8->9->10->1->3->4->6->1 -> 具有 List1 和 List2 交集的鏈表:1->
uj5u.com熱心網友回復:
函式內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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/537844.html
標籤:C指针数据结构链表单链表
上一篇:鏈表的C 指標演算法
下一篇:清除陣列以準備新的陣列值
