In: Computer Science
Below is for C language
typedef struct _node {
Node *next;
char *str;
} Node;
You are given a function that takes in two Node* pointers to two different linked lists. Each linked list node has a string represented by a character array that is properly null terminated. You're function is supposed to return true if the concatenation of strings in a linked list match each other, else return false.
EXAMPLES
List 1: "h" -> "el" -> "l" -> "o" -> NULL
List 2: "he" -> "llo" -> NULL
Return: True
List 1: "je" -> "lc -> "l" -> "o" -> NULL
List 2: "h" -> "e" -> "ll" -> "" -> "o" -> NULL
Return: False
List 1: “e” -> “llo” -> “h” -> NULL
List 2: “h” -> “e” -> “l” -> “l” -> “o” -> NULL
Return: False
bool areListsMatching(Node *list1, Node *list2) {
// TODO: Fill this
}
// below is c code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
struct Node
{
char ch[30];
struct Node *next;
};
struct Node* newNode(char ch[])
{
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
char a[30];
int i=0;
for(i=0;ch[i]!='\0';i++)
{
temp->ch[i]=ch[i];
}
temp->next = NULL;
return temp;
};
bool areListsMatching(struct Node *llist1, struct Node *llist2)
{
struct Node *temp1=llist1 ;
struct Node *temp2=llist2;
char str1[200];
char str2[200];
while(temp1!=NULL)
{
strcat(str1,temp1->ch);
temp1=temp1->next;
}
while(temp2!=NULL)
{
strcat(str2,temp2->ch);
temp2=temp2->next;
}
if(strcmp(str1,str2)==0)
{
return true;
}
return false;
}
int main()
{
struct Node *llist1 = newNode("h");
llist1->next = newNode("el");
llist1->next->next = newNode("l");
llist1->next->next->next = newNode("o");
struct Node *llist2 = newNode("h");
llist2->next = newNode("e");
llist2->next->next = newNode("l");
llist2->next->next->next = newNode("l");
llist2->next->next->next->next = newNode("o");
printf("%d",areListsMatching(llist1, llist2));
return 0;
}
//output