1. Write a function that finds the length of a list of list_node_t nodes. Functi
ID: 3535817 • Letter: 1
Question
1. Write a function that finds the length of a list of list_node_t nodes.
Function prototype:
int length(list_node_t *headp);
2. Write a recursive version of function search (see iterative function search on page 721)
prototype:
list_node_t *search(list_node_t *headp, int target);
Searches a list for a specified target value. Returns a pointer to the first node containing target if found. Otherwise returns NULL.
type list_node_t is typedefed on page 716:
typedef struct list_node_s {
int digit;
struct list_node_s *restp;
} list_node_t;
Explanation / Answer
int length(struct list_node_t *headp)
{
int len=0;
struct node *t;
if( headp == NULL)
return(0);
t = headp;
while( t != NULL )
{
len++;
t = t->next;
}
return(len);
}
list_node_t *search(list_node_t *headp, int target)
{
list_node_t *cur_nodep; /* pointer to node currently being checked */
for(cur_nodep = headp;cur_nodep!= NULL && cur_nodep->digit != target;cur_nodep = cur_nodep->restp)
{}
return (cur_nodep);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.