Thread: problem in deleting and node from linked list

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    23

    Question problem in deleting and node from linked list

    Hi,
    I have this code:

    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;
        }
      }
    }
    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.
    You probably should look at the two functions: void fourth_main() and int_list_s* del_odd(int_list_s** headRef).
    Please assist.
    thanks

  2. #2
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Hi,

    Some side notes:
    Please do not cast malloc function. Also your usage of "main" in several functions names is strange. Also you better use default in your switch statement. By the way your indentation has problems too. Also rather than asking the user for things like "row" and "column" you can automatically compute them.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Your source code is upside down. Higher level functions should go above lower level functions. I was going to read the code but now I can't Having said that, it's pretty long for a simple linked list :/ Also, where is your delete node function?

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Quote Originally Posted by Hodor View Post
    Your source code is upside down. Higher level functions should go above lower level functions. I was going to read the code but now I can't Having said that, it's pretty long for a simple linked list :/ Also, where is your delete node function?
    OP can use function declaration.

  5. #5
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Quote Originally Posted by ordak View Post
    Hi,

    Some side notes:
    Please do not cast malloc function. Also your usage of "main" in several functions names is strange. Also you better use default in your switch statement. By the way your indentation has problems too. Also rather than asking the user for things like "row" and "column" you can automatically compute them.
    Thanks, I know that my code is a mess and somebody told me already about the malloc function, I will implement this next time that I write code.
    For now I have to submit this to my teacher in two hours so I don't have time to edit this... Thanks anyway

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Quote Originally Posted by Hodor View Post
    Your source code is upside down. Higher level functions should go above lower level functions. I was going to read the code but now I can't Having said that, it's pretty long for a simple linked list :/ Also, where is your delete node function?
    I don't know what you mean by high/low level functions. I will google it after I submit this home work. Thanks.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Firstly, just ignore Hodor's "advice". It's just an opinion, and I totally disagree with it.

    I was going to point out numerous errors in your code, but since you don't have time to edit it I will save myself the trouble.
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  8. #8
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Quote Originally Posted by algorism View Post
    Firstly, just ignore Hodor's "advice". It's just an opinion, and I totally disagree with it.

    I was going to point out numerous errors in your code, but since you don't have time to edit it I will save myself the trouble.
    I am aware that the code is far from perfect and I remember the "tips" that you gave me in my other thread.
    Can you assist me with this one?

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by algorism View Post
    Firstly, just ignore Hodor's "advice". It's just an opinion, and I totally disagree with it.

    I was going to point out numerous errors in your code, but since you don't have time to edit it I will save myself the trouble.
    It's an opinion yes, but I think it's a fairly wide-held opinion. What do you disagree with? Also, it's pretty hard to argue that there should not be a delete node function.

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Hodor View Post
    It's an opinion yes, but I think it's a fairly wide-held opinion. What do you disagree with? Also, it's pretty hard to argue that there should not be a delete node function.
    I disagree with your pointless personal opinion about the ordering of functions. And, no, it's not "wide-held".
    I never said anything about a delete node function.
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by algorism View Post
    I disagree with your pointless personal opinion about the ordering of functions. And, no, it's not "wide-held".
    I never said anything about a delete node function.
    I don't think it's pointless at all. Having functions ordered in a top-down manner makes reading the code easier. Most people, companies and projects adhere to this practice. As an added bonus you have to use function prototypes so you can very quickly see what functions are present without having to slowly scroll through the code. It seems like a pretty funny thing to disagree with, but each to their own.

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Hodor View Post
    I don't think it's pointless at all. Having functions ordered in a top-down manner makes reading the code easier. Most people, companies and projects adhere to this practice. As an added bonus you have to use function prototypes so you can very quickly see what functions are present without having to slowly scroll through the code. It seems like a pretty funny thing to disagree with, but each to their own.
    Look, moron, I got the last word!
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting a node from a linked list.
    By Mr.Lnx in forum C Programming
    Replies: 15
    Last Post: 10-22-2013, 01:55 AM
  2. Replies: 5
    Last Post: 04-07-2009, 05:51 PM
  3. deleting node in linked list
    By cstudent in forum C Programming
    Replies: 1
    Last Post: 05-15-2008, 02:42 AM
  4. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  5. Problem deleting a node from a linked list
    By Dag_ in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 09:53 AM

Tags for this Thread