Question

This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node...

This is a recursive function to delete all even nodes from a linked list.

node *deleteEven(node *head)

if (head == NULL)

   return head;

  

if (head->data % 2 == 0)

{

node *temp = head;

free(temp);

return deleteEven(head->next);

}

else

{

head->next = deleteEven(head->next);

return head;

}

}

What is the purpose of heaving to set "head->next = deleteEven(head->next);" instead of just having "head = head->next;" in one of the lines above and just calling "deleteEven(head->next);"?

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

First of all there is a bug in the code, look at the code block of second if statement. After temp is assigned the address to which head is pointing, you have used free(temp). free( ) is used to free the memory location of the data so that it can be used by another program or function call. In some cases, the data is not immediately freed, and your program would work fine, since the address of next node can be found by using head->next. But in case the memory block is immediately used by another program or function call, then your program may crash because head is still pointing to this node which would now have some arbitrary data, and hence head->next would be some unknown memory location. Therefore, you should save head->next before using free.

Now let's analyse the correct code to answer your query.

Consider the following linked list (head = 100 is passed from main.)

Let's us look at the code with changes specified by you in block 3:

From head = 200, you directly passed head = 600 which means head = 400 would never be passed. Now at location 400, data can either be odd or even, this particular node would never be passed on , therefore breaking links. When you would backtrack, you would never be able to create links because they would be lost.

Introducing head = head->next is moving head one position and saving this new position in head. Now while calling deleteEven(head->next) you are again moving one position, so in all you moved two locations leaving one node unaddressed.

Therefore, the above code with mentioned corrections would work fine.

Hope it helps.

Add a comment
Know the answer?
Add Answer to:
This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node...
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
  • 1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...

    1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...

  • Question 2 (3 points) Given the following singly linked list (a list with four nodes), what...

    Question 2 (3 points) Given the following singly linked list (a list with four nodes), what will be the value stored at the last node when the following operations are executed? head->1111-151->11011->1211 Node curr = head; while(curr next !=null) { curr.item = curritem-head. item; curr = cur next; اسرة Node newNode = new Node(8): curr.next = newNode: 23 O 12 OS O21 Question 3 (3 points) Given a recursive method as shown below, what is the return value for P(5)...

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

  • Exercise: Delete First Element from a Linked List (pair) This is a pair exercise to complete...

    Exercise: Delete First Element from a Linked List (pair) This is a pair exercise to complete with your lab partner. Download list_delete_first.c here, or copy it to your CSE account using the following command: cp -n /web/dp1091/20T1/activities/list_delete_first/list_delete_first.c . Your task is to add code to this function in list_delete_first.c: // // Delete the first node in list. // The deleted node is freed. // The head of the list is returned. // struct node *delete_first(struct node *head) { // PUT...

  • do the following in Node* buildLinkedList() function 1. Build linked list of 3 nodes. 2. Ask...

    do the following in Node* buildLinkedList() function 1. Build linked list of 3 nodes. 2. Ask the user for the data values in the structure. 3. Follow the example above on how to build linked list. Make sure you ask the user for the data values. 4. Return head. in Main do the following: 1. Declare a pointer to structure of type Node. 2. Call the function buildLinkedList. 3. Display the 3 nodes data values.

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

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