Hi,

I have this code:

I have the function " int_list_s* del_odd(int_list_s** headRef) " that should delete all the odd numbers from the linked list (head) by reference and create new linked list ( odd_list_head ) with the numbers that i deleted. I can't figure what I'm doing wrong.Code:#include <stdio.h> #include <stdlib.h> // mat1 #define col_size1 3 #define row_size1 2 // mat2 #define col_size2 4 #define row_size2 3 // struct of i,j,arr[i][j] typedef struct{ int i_index; int j_index; int value; }index_s; // struct of index_s linked list Node typedef struct node_s{ index_s data; struct node_s* next; }node_s; // struct of int list typedef struct int_list_s{ int data; struct int_list_s* next; }int_list_s; // print_array void print_array(unsigned int* arr, int size){ int i; printf("\nprint_array:\n"); for(i=0; i<size; i++){ printf("arr[%d] = %u\n", i, *(arr+i)); } } // free_matrix void free_matrix(int** c, int n){ int i; for(i=0; i<n; i++){ free(c[i]); } free(c); } // alloc_matrix int** alloc_matrix(int rows, int cols){ int** c; int i; int j; c = (int**)calloc(rows,sizeof(int*)); if(!c) return NULL; for(i=0; i<rows; i++){ c[i] = (int*)calloc(cols,sizeof(int)); if(!c[i]){ free_matrix(c,i); return NULL; } for(j=0; j<cols; j++){ printf("arr[%d][%d]= ",i,j); scanf("%d",&c[i][j]); } } return c; } // print_din_matrix void print_din_matrix(int** a, int rows, int cols){ int i,j; for(i=0; i<rows; i++){ for(j=0; j<cols; j++){ printf("%d\t",a[i][j]); } printf("\n"); } } // node_s* push node_s* push(node_s* headRef, index_s data){ node_s* newNode = malloc(sizeof(node_s)); newNode->data = data; newNode->next = NULL; if(headRef != NULL){ newNode->next = headRef; } headRef = newNode; return headRef; } // print node_s list void print_list(node_s* headRef){ while(headRef != NULL){ printf("i=%d\t j=%d\t value=%d\t\n", (*headRef).data.i_index,(*headRef).data.j_index,(*headRef).data.value); headRef = headRef->next; } printf("\n"); } // int push int_list_s* int_push(int_list_s** headRef, int data){ int_list_s* newNode = malloc(sizeof(int_list_s)); newNode->data = data; newNode->next = NULL; if(*headRef == NULL){ *headRef = newNode; return *headRef; } if((*headRef)->next == NULL){ (*headRef)->next = newNode; } else{ int_list_s* current = *headRef; while(current->next != NULL){ current = current->next; } current->next = newNode; } return *headRef; } // print int list void print_int_list(int_list_s* headRef){ int i = 1; while(headRef != NULL){ printf("node %d = %d\n", i, headRef->data); headRef = headRef->next; i++; } } // free linked_list void free_int_list(int_list_s* headRef){ int_list_s* temp; while(headRef != NULL){ temp = headRef; headRef = headRef->next; free(temp); } } // taks 1 unsigned int* powerArray(int power){ unsigned int* p; int i=1; p = (unsigned int*)malloc(power*(sizeof(unsigned int))); *p = 1; for(i; i<power; i++){ *(p+i) = *(p+i-1)*2; } return p; } void first_main(){ unsigned int* p; int power; printf("Enter power: "); scanf("%d", &power); p = powerArray(power); print_array(p, power); } // task 2 int** matrix_mul(int mat1[row_size1][col_size1], int mat2[row_size2][col_size2]){ int i = 0; int j = 0; int k = 0; int sum = 0; int** mat = (int**)malloc(row_size1 * sizeof(int*)); for(i; i<row_size1; i++){ mat[i] = (int*)malloc(col_size2 * sizeof(int)); } for(i=0; i<row_size1; i++){ for(j=0; j<col_size2; j++){ for(k=0; k<col_size1; k++){ sum += mat1[i][k] * mat2[k][j]; } mat[i][j] = sum; sum = 0; } } return mat; } void second_main(){ int mat1[row_size1][col_size1] = {{4,1,2},{6,0,3}}; int mat2[row_size2][col_size2] = {{5,7,2,9},{8,3,4,0},{6,1,2,7}}; int** arr; arr = matrix_mul(mat1, mat2); print_din_matrix(arr, row_size1, col_size2); } // task 3 int index_sum(int** arr, int rows, int cols, index_s** ans_arr, node_s** headRef){ int i; int j; int ans = 0; int arr_i = 0; for(i=0; i<rows; i++){ for(j=0; j<cols; j++){ if ((i+j) == arr[i][j]){ ans++; } } } *ans_arr = (index_s*)malloc(ans*sizeof(index_s)); for(i=0; i<rows; i++){ for(j=0; j<cols; j++){ if((i+j) == arr[i][j]){ (*ans_arr)[arr_i].i_index = i; (*ans_arr)[arr_i].j_index = j; (*ans_arr)[arr_i].value = arr[i][j]; arr_i++; } } } for(i=0; i<ans; i++){ *headRef = push(*headRef, (*ans_arr)[i]); printf("\n"); } return ans; } void third_main(){ int** arr; index_s* ans_arr; int rows, cols; int num_of_sums; node_s* head = NULL; printf("Enter number of rows: "); scanf("%d",&rows); printf("Enter number of cols: "); scanf("%d",&cols); arr = alloc_matrix(rows, cols); num_of_sums = index_sum(arr, rows, cols, &ans_arr, &head); int i; printf("Array print:\n"); for(i=0; i<num_of_sums; i++){ printf("i=%d\t j=%d\t value=%d\n", (ans_arr)[i].i_index, (ans_arr)[i].j_index, (ans_arr)[i].value); } printf("\n\nLinked list print:\n"); print_list(head); printf("\nThe function returns:\nAns = %d\n", num_of_sums); } // task 4 int_list_s* del_odd(int_list_s** headRef){ int i; int_list_s* temp = NULL; int_list_s* previous = NULL; if(*headRef == NULL){ return NULL; } if((*headRef)->data % 2 != 0){ temp = int_push(&temp, (*headRef)->data); headRef = &((*headRef)->next); } while((*headRef)->next != NULL){ if((*headRef)->data % 2 == 0){ *headRef = (*headRef)->next; } else{ temp = int_push(&temp, (*headRef)->data); printf("\n%d\n",temp->data); previous = (*headRef)->next; (*headRef)->next = previous->next; } } return temp; } void fourth_main(){ int_list_s* head = NULL; int_list_s* odd_list_head; int data; int list_size; int i; printf("Please enter the size of the list: "); scanf("%d", &list_size); for(i=0; i<list_size; i++){ printf("Enter node #%d: ", i); scanf("%d",&data); head = int_push(&head, data); } if(list_size == 0 ){ printf("The list is empty\n"); return; } print_int_list(head); odd_list_head = del_odd(&head); printf("\nOdd_list_head:\n"); print_int_list(odd_list_head); printf("\nhead:\n"); print_int_list(head); } int main(){ int choice; while(1){ printf("# Please select the exercise (1-5, 0 to exit): "); scanf("%d",&choice); if (choice == 0){ printf("Good bye\n"); return 0; } switch (choice) { case 1: first_main(); printf("\n"); break; case 2: second_main(); printf("\n"); break; case 3: third_main(); printf("\n"); break; case 4: fourth_main(); printf("\n"); break; } } }

You probably should look at the two functions: void fourth_main() and int_list_s* del_odd(int_list_s** headRef).

Please assist.

thanks