Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi! I have the following exercise for programming with c as well as given code in which the logic should be implemented at the TODOs

Hi!

I have the following exercise for programming with c as well as given code in which the logic should be implemented at the "TODOs" (see below for both). Can you help me to implement the code? Thank you very much in advance!

Exercise:

The int_to_linked_list function stores the individual digits of a passed positive integer greater than or equal to 0 in a linked list. Starting from the start node of the list, the values of the nodes have the digits of the number read from right to left.

The free_linked_list function frees the memory which was reserved for the linked list.

Implement the following functions:

print_linked_list: Prints the elements of the linked list starting from the head of the list to the end of the list on standard output. The individual elements are to be output as numbers. No separators are inserted between the individual numbers.

print_number_rec: Prints the stored number based on elements of the linked list. The calculation or output of the stored number must be done without loops and recursively. For this purpose, the print_number_rec function can call a recursive helper function, for example.

print_number_iter: prints the stored number based on elements of the concatenated list. The calculation or output of the stored number must be iterative.

For these functions, output only the digits. Make sure to test your implementation in the main function!

Code:

#include  struct list_node { unsigned value : 4; struct list_node *next; }; struct linked_list { size_t size; struct list_node *head; }; void free_linked_list(struct linked_list *list) { if (!list) { return; } struct list_node *node = list->head; while (node) { struct list_node *next_node = node->next; free(node); node = next_node; } free(list); } struct linked_list *int_to_linked_list(int number) { if (number < 0) { return NULL; } struct linked_list *list = malloc(sizeof(*list)); if (!list) { return NULL; } list->head = NULL; list->size = 0; struct list_node **last_node = &list->head; while (number || !list->size) { struct list_node *current_node = malloc(sizeof(*list)); if (!current_node) { free_linked_list(list); return NULL; } current_node->next = NULL; current_node->value = number % 10; number /= 10; *last_node = current_node; last_node = ¤t_node->next; ++list->size; } return list; } void print_linked_list(struct linked_list *list) { /* TODO: Implement function logic */ } void print_number_rec(struct linked_list *list) { /* TODO: Implement function logic */ } void print_number_iter(struct linked_list *list) { /* TODO: Implement function logic */ } int main(void) { /* TODO: Implement Tests */ return EXIT_SUCCESS; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions

Question

How does planning affect the success of a business?

Answered: 1 week ago