struct - C break int to digits and store it in linked list -
i have exercise on linked lists.
i have managed create_list()
, print_number()
functions, can't seem manage listnode_t *int_to_list(int num)
. note exercise based on code template, see (main
, functions arguments) prefixed.
we supposed fill in code inside function code compiles , runs without problems.
here's code:
#include <stdio.h> #include <stdlib.h> typedef struct node { int digit; struct node *next; } listnode_t; listnode_t *create_list(int numdigits); void print_number(listnode_t *head); listnode_t *int_to_list(int num); void freelist(listnode_t * head) { listnode_t * currentnode = head; listnode_t * node_to_free = null; while(currentnode->next != null) { node_to_free = currentnode; currentnode = currentnode->next; free(node_to_free); } } int main(int argc, char** argv) { listnode_t *number1, *number2, *result; printf("insert number\n"); number1 = create_list(3); printf("insert number\n"); number2 = create_list(5); printf("first number: "); print_number(number1); printf("\n"); printf("second number: "); print_number(number2); printf("\n"); // convert int list number1 = int_to_list(1024); print_number(number1); printf("\n"); freelist(number1); freelist(number2); return 0; } listnode_t * create_list(int numdigits) { int number[numdigits]; int i; for(i=0; < numdigits; i++) { printf("dose %d stoixeio tou akeraiou: ", + 1); scanf("%d", &number[i]); } listnode_t * head = (listnode_t *)malloc(sizeof(listnode_t)); if(head == null) { return null; } head->digit = number[numdigits - 1]; head->next = null; listnode_t * currentnode = head; for(i = numdigits - 2; >= 0; i--) { listnode_t * node = (listnode_t *)malloc(sizeof(listnode_t)); node->digit = number[i]; node->next = null; currentnode->next = node; currentnode = node; } return head; } void print_number(listnode_t * head) { listnode_t * currentnode = head; while(currentnode != null) { printf("%d", currentnode->digit); currentnode = currentnode->next; } } /* function takes integer , converts list. functio returns pointer first node of list.*/ listnode_t *int_to_list(int num) { int newnum; listnode_t * head = malloc(sizeof(listnode_t)); listnode_t * current = head; current->next = malloc(sizeof(listnode_t)); while (current->next != null) { current = current->next; } newnum = num % 10; head->digit=newnum; head->next=current; num = num / 10; while (num > 1 && num < 9) { current->digit=newnum; current->next=null; } newnum = num % 10; return head; }
i appreciate help.
given input 1024
shall create new list this:
(1, *)---->(0, *)---->(2, *)---->(4, null)
so need build list:
listnode_t *int_to_list(int num) { listnode_t * head = null; listnode_t * current; int first_time = 1; while( not_done ) { if (first_time) { first_time = 0; head = malloc(sizeof(listnode_t)); current = head; } else { current->next = malloc(sizeof(listnode_t)); current = current->next; } current->next = null; current->digit = current_digit; } return head; }
what missing here not_done
, current_digit
i'll leave op figure out. can done in several ways. 1 approach use sprintf
convert int
string (e.g. "1024"
) , take digits there str[i] - '0'
.
Comments
Post a Comment