Question

Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

Please write the code in a text editor and explain thank you!

1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will not receive any credit. You must comment your code. Below is the declaration of the function:

bool findValueRecursively(Node *head, int value);

As an example if the linked-list contains the elements [10]->[1]->[50]->[10]->null and the function is called passing the head of the list (address of node with value 10) and the value to find to be 10, it should return true. If the function is called on the same list passing the value 40, it should return false. For an empty list it should return false.

2. C-STRINGS: Implement a function that takes as input a const C string containing

only alphabets. The function should allocate memory for a new string on the heap and copy into it the original string converting all upper case letters to lowercase. The function should then return the new string containing on lower case letters. Below is the declaration of the function you are expected to write: char* ToLower(const char * s);

In your implementation, you may use any or all of the following functions that operate on C-strings int strlen(char *s);​//takes a C-string as input and returns the length of the string not counting the null terminator
char tolower(char c);​ //takes a single character as input and returns the lower-case version of the character. You may also use other C-string functions covered in class as appropriate.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here I have attached C function for the problems given above:

1.

bool findValueRecursively(struct node *head, int value){

//if head is present and head's data is equal to given value, we return true
if(head){
if(head->data == value){
return true;
}
else{

//if head is present and head's data is not equal to given value, we go to next node
return (findValueRecursively(head->next, value));
}
}

//if we cant find the given value or list is empty, we return false.
return false;
}

2.

char* ToLower(const char * s){
int len,i;
len = strlen(s); //strlen() will return length of the string
char *newString = (char *)malloc((len+1)*sizeof(char)); //creating memory of new string of length of given string s
for(i = 0; i < len ; i++){
newString[i] = tolower(s[i]);
}
newString[i]='\0';
//returning newn string
return newString;
}

Hoping that the above solution helps. Please comment for further assistance.

Add a comment
Know the answer?
Add Answer to:
Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT