C语言 如何找到字符串链表的并集和交集?



我试图编码2链表的联合和交集。然而,我没有成功地创建字符串链表,因为它的功能无法显示,也无法将节点插入列表中。代码只能读取链表1的字符串。我想知道创建字符串链表的方法。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char* data;
struct node *next;
} NODE;
NODE* create(char* data){
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
void insertAtBeg(NODE **head, char* data){
NODE *newnode = create(data);
newnode->data = data;
newnode->next = (*head);
(*head) = newnode;
}
void display(NODE *temp){
while(temp != NULL){
printf("%d->",temp->data);
temp = temp->next;
}
}
int isPresent(NODE *head, char* data){
NODE *temp = head;
while(temp != NULL){
if(temp->data == data)
return 1;
temp = temp->next;
}
return 0;
}
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* head1 = NULL;
NODE* head2 = NULL;
NODE* intersection = NULL;
NODE* unin = NULL;
int m,n;
char *arr;
printf("Enter the size of the linked list1:n");
scanf("%d",&m);
printf("Enter the elements:n");

for(int i = 0; i< m ; i++){
scanf("%s",arr[i]);
insertAtBeg(&head1,arr[i]);
}
printf("nDisplaying linked list1:n");
display(head1);
printf("nEnter the size of linked list 2:n");
scanf("%d",&n);
printf("nEnter the elements:n");
for(int i = 0; i< n ; i++){
scanf("%s",arr[i]);
insertAtBeg(&head2,arr[i]);
}
printf("nDisplaying linked list2:n");
display(head2);
unin = getUnion(head1,head2);
intersection = getIntersection(head1,head2);
printf("nUnion list:n");
display(unin);
printf("nIntersection list:n");
display(intersection);

return 0;
}

错误:

warning: passing argument 2 of ‘insertAtBeg’ makes pointer from integer without a cast [-Wint-conversion]
84 |         insertAtBeg(&head1,arr[i]);
|                            ~~~^~~
|                               |
|                               char
q.c:16:37: note: expected ‘char *’ but argument is of type ‘char’
16 | void insertAtBeg(NODE **head, char* data){
|                               ~~~~~~^~~~
q.c:94:31: warning: passing argument 2 of ‘insertAtBeg’ makes pointer from integer without a cast [-Wint-conversion]
94 |         insertAtBeg(&head2,arr[i]);
|                            ~~~^~~
|                               |
|                               char
q.c:16:37: note: expected ‘char *’ but argument is of type ‘char’
16 | void insertAtBeg(NODE **head, char* data){

代码的几个问题,所以我做了一些改变,至少,指出一个问题,还放置了printf语句来帮助调试(需要在迭代开发代码时完成)。

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char* data;
struct node *next;
} NODE;
NODE* create(char* data){
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
void insertAtBeg(NODE **head, char* data){
printf("<in insertAtBeg(NODE **head, char* data) n");
NODE *newnode = create(data);
newnode->data = data;
newnode->next = (*head);
(*head) = newnode;
}
void display(NODE *temp){
while(temp != NULL){
printf("%d->",temp->data);
temp = temp->next;
}
}
int isPresent(NODE *head, char* data){
NODE *temp = head;
while(temp != NULL){
if(temp->data == data)
return 1;
temp = temp->next;
}
return 0;
}
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* head1 = NULL;
NODE* head2 = NULL;
NODE* intersection = NULL;
NODE* unin = NULL;
int m,n;
char *arr;
NODE* result = NULL;
insertAtBeg(&head2,&arr[0]);
insertAtBeg(&result, "c"); // t1->data); was data allocated memory?
/*
printf("Enter the size of the linked list1:n");
scanf("%d",&m);
printf("Enter the elements:n");

for(int i = 0; i< m ; i++){
scanf("%s",arr[i]);
insertAtBeg(&head1,arr[i]);
}
printf("nDisplaying linked list1:n");
display(head1);
printf("nEnter the size of linked list 2:n");
scanf("%d",&n);
printf("nEnter the elements:n");
for(int i = 0; i< n ; i++){
scanf("%s",arr[i]);
insertAtBeg(&head2,arr[i]);
}
printf("nDisplaying linked list2:n");
display(head2);
unin = getUnion(head1,head2);
intersection = getIntersection(head1,head2);
printf("nUnion list:n");
display(unin);
printf("nIntersection list:n");
display(intersection);
*/
return 0;
}
Output:    
<in insertAtBeg(NODE **head, char* data) 
<in insertAtBeg(NODE **head, char* data) 

相关内容

  • 没有找到相关文章

最新更新