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

Giving the following: typedef node Node; struct node{ int data; Node* next; }; 1

ID: 3726156 • Letter: G

Question

Giving the following:

typedef node Node;

struct node{

int data;

Node* next;

};

1. Write a function called destory that takes a Node pointer to head of a list and will free up the memory associated with each node in entire list.

2. Write a recursive function called sum that given a Node pointer to the head of a list will return the sum of all data in the linked list.

3. Write a function called copy_list that, given a Node pointer to the head of a list will return a Node pointer containing the address of the head node of a new list that is an exact copy of the oranginal list. Your copy should be independent of the first list and not share any nodes. You may write an iterative or recursive version of your function.

Explanation / Answer

If you post more than 1 question, as per chegg guidelines I have to solve only 1 question.

Ques 1.

void destroy(Node *head)

{

    // point trav to the head of the list

    Node *trav = head;

   

    // traverse through the list

    while( trav )

    {

      Node *curr = trav;

       

        // move to the next node

        trav = trav->next;

       

        free(curr);

    }

}

Ques 2.

int sum(Node *head)

{

    // point trav to the head of the list

    Node *trav = head;

   

    int ans = 0;

   

    // traverse through the list

    while( trav )

    {

        ans += trav->data;

       

        // move to the next node

        trav = trav->next;

    }

   

    return ans;

}

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