Question: Write the following function for the given program (below) linked_list.c: int count_n(struct node *list, int n); The list parameter points to a linked list. The
Write the following function for the given program (below) linked_list.c:
int count_n(struct node *list, int n);
The list parameter points to a linked list. The function should return the number of nodes that contains n; it should return 0 if n doesnt appear in the list. Assume that the node structure is declared as:
struct node {
int value; /* data stored in the node */
struct node *next;
/* pointer to the next node */
};
Download linked_list.c on Canvas, fill in the code for the function, test your function with input 3 (there are three 3), 64 (there are two 64), and other numbers.
Add the following function to the program:
struct node *find_largest(struct node *list);
The list parameter points to a linked list. The function should return a pointer to the node that contains the largest value; it should return NULL if the list is empty.
Add statements in the main function to test it.
Code for linked_list.c provided below:
#include
struct node *add_to_list(struct node *list, int n); struct node *search_list(struct node *list, int n); struct node *delete_from_list(struct node *list, int n); int count_n(struct node *list, int n); void clearList(struct node *list);
struct node { int value; /* data stored in the node */ struct node *next; /* pointer to the next node */ };
int main() {
struct node *first = NULL; int number, count = 0;
/* inserting nodes */ first = add_to_list(first, 3); first = add_to_list(first, 64); first = add_to_list(first, 98); first = add_to_list(first, 3); first = add_to_list(first, 64); first = add_to_list(first, 3); first = add_to_list(first, 136);
printf("Enter a number: "); scanf("%d", &number); count = count_n(first, number); printf("The number of nodes that contains %d is %d. ", number, count); clearList(first); return 0;
}
struct node *add_to_list(struct node *list, int n) { struct node *new_node; new_node = malloc(sizeof(struct node)); if (new_node == NULL) { printf("malloc failed in add_to_list "); return list; } new_node->value = n; new_node->next = list; return new_node; }
struct node *search_list(struct node *list, int n) { struct node *p; for (p = list; p != NULL; p = p->next) if (p->value == n) return p; return NULL; }
struct node *delete_from_list(struct node *list, int n) { struct node *cur, *prev; for (cur = list, prev = NULL; cur != NULL && cur->value != n; prev = cur, cur = cur->next) ; if (cur == NULL) return list; /* n was not found */ if (prev == NULL) list = list->next; /* n is in the first node */ else prev->next = cur->next; /* n is in some other node */ free(cur); return list; }
void clearList(struct node *list) { struct node *p; while(list != NULL) { p = list; list = list->next; if( p!= NULL) free(p); } } int count_n(struct node *list, int n){ return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
