Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ListyString *listyCat(ListyString *listy, char *str); Description: Concatenate s

ID: 3722552 • Letter: L

Question

ListyString *listyCat(ListyString *listy, char *str);
Description: Concatenate str to the end of the linked list string inside listy. If str is either NULL or the empty string (“”), then listy should remain unchanged. Be sure to update the length member of listy as appropriate.
Special Considerations: If listy is NULL and str is a non-empty string, then this function should create a new ListyString that represents the string str. If listy is NULL and str is NULL, this function should simply return NULL. If listy is NULL and str is a non-NULL empty string (“”), then this function should return a ListyString whose head member has been initialized to NULL and whose length member has been initialized to zero.
Runtime Requirement: The runtime of this function must be no worse than O(n+m), where n is the length of listy and m is the length of str.
Returns: If this function caused the creation of a new ListyString, return a pointer to that new ListyString. If one of the special considerations above requires that a NULL pointer be returned, then do so. Otherwise, return listy.

Inside H file:

#ifndef __LISTY_STRING_H

#define __LISTY_STRING_H

typedef struct ListyNode

{

// Each node holds a single character.

char data;

// Pointer to next node in linked list.

struct ListyNode *next;

} ListyNode;

typedef struct ListyString

{

// Head of the linked list representing this string.

ListyNode *head;

// Length of this linked list.

int length;

} ListyString;

ListyString *listyCat(ListyString *listy, char *str);

Explanation / Answer

// Code of the required function is as follows

// P.S. As there was no time to give step by step information of the steps I have given brief about

// the specific block of code

// overall the code is fairly complex but easy to understand

// still if more information is required please let me know

// Do comment and like if the answer is helpful

// Give feedback for improvements

ListyString *listyCat (ListyString * listy, char *str)

{

ListyString *tempList;

// Following if conditions are used to fullfil the special conditions

if (!listy)

{

if (str[0] != '')

{

ListyString tempL;

tempL.head = NULL;

tempL.length = 0;

tempList = &tempL;

}

else if (!str)

{

tempList = NULL;

}

else if (str[0] == '')

{

ListyString tempL;

tempL.head = NULL;

tempL.length = 0;

tempList = &tempL;

}

}

else

{

tempList = listy;

tempList->head = listy->head;

}

// From the next line onwards we have written the code for adding the str to the end of the current ListyString

ListyNode *temp;

if (!listy)

temp = tempList->head;

else

{

temp = tempList->head;

}

while (!temp->next)

temp = temp->next;

int i = 0;

while (str[i] != '' && str != NULL && str[0] != '')

{

ListyNode new;

temp->next = (struct ListyNode*) malloc(sizeof(struct ListyNode));

temp->next->data = *(str + i);

temp->next->next = (struct ListyNode*) malloc(sizeof(struct ListyNode));

temp = temp->next;

(tempList->length)++;

i++;

}

return tempList;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote