Question

In: Computer Science

Below is for C language typedef struct _node { Node *next; char *str; } Node; You...

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

}

Solutions

Expert Solution

// 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


Related Solutions

Below is for C language typedef struct _node { Node *next; char *str; } Node; You...
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"...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
C++ #include <iostream> using namespace std; struct Node {     int data;     Node* next;   ...
C++ #include <iostream> using namespace std; struct Node {     int data;     Node* next;        Node(){         data = 0;         next = NULL;     }        Node(int x){         data = x;         next = NULL;     } }; struct LinkedList {     Node* head;        LinkedList(){         head = NULL;     }        void append(int value){                if (head == NULL){             head = new Node(value);         }         else{                       ...
FINISH print and freelist #include<stdio.h> #include <stdlib.h> struct node {      int data;      struct node  *next; }; struct...
FINISH print and freelist #include<stdio.h> #include <stdlib.h> struct node {      int data;      struct node  *next; }; struct node* insert(struct node* list,int d ); struct node* del(struct node* list,int d ); void print( struct node *list); void freeList(struct node* list); void copy ( struct node *q, struct node **s ); int main( ) {     int number = 0, choice = 0;     struct node *pList=NULL;     struct node *nList = NULL;         while(choice!= 4)     {                 printf("Do you want to (1)insert, (2)delete, (3)Copy (4)quit.\n");...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
Assuming: struct node { int data; node * next; }; and addList(node * head); Write a...
Assuming: struct node { int data; node * next; }; and addList(node * head); Write a function to recursively add to the end of a linear linked list.
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT