Thread: linked list doesn't stop printing

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

    Question linked list doesn't stop printing

    Hi,
    I try to add some data to linked list and print it but it doesn't stop printing.. Can you assist please?

    This is the code:

    main:
    Code:
    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;
        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);
    }
    index sum:
    Code:
    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++;
                }
            }
        }
    
    
        *headRef = malloc(sizeof(node_s));
        for(i=0; i<ans; i++){
          push(headRef, (*ans_arr)[i]);
        }
    
    
        return ans;
    }
    print list:
    Code:
    void print_list(node_s* headRef){
      node_s* current = headRef;
      while(current != NULL){
        printf("%d %d %d", (*current).data.i_index);
      }
    }
    push:
    Code:
    void push(node_s** headRef, index_s data){
      node_s* newNode = malloc(sizeof(node_s));
      newNode->data = data;
      newNode->next = *headRef;
      *headRef = newNode;
      printf("%d", (*headRef).data.i_index);
    }
    structs:
    Code:
    typedef struct{
        int i_index;
        int j_index;
        int value;
    }index_s;
    
    
    
    
    // struct of linked list Node
    typedef struct node_s{
       index_s data;
       struct node_s* next;
    }node_s;

    Thanks
    Last edited by DoK; 08-26-2017 at 03:27 AM. Reason: added structs

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Delete this line
    *headRef = malloc(sizeof(node_s));

    Your print loop needs a
    current = current -> next;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    Delete this line
    *headRef = malloc(sizeof(node_s));

    Your print loop needs a
    current = current -> next;
    still not working :\

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Your compiler should warn you about the print_list() function.
    The printf() inside the function has to less arguments.
    I have also edited the arguments itself to use the arrow-operator.

    Code:
    void print_list(node_s* headRef) {
      node_s* current = headRef;
      while(current != NULL) {
        printf("list: %d %d %d\n", current->data.i_index, current->data.j_index, current->data.value);
        current = current->next;
      }
    }
    Other have classes, we are class

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why in the world would you post all of the functions separately? That's idiotic. It's just more work for both you and us (if we want to run it). Post full, runnable programs whenever possible.

    And "still not working" is even more stupid. "Not working" how? Obviously Salem's answer would fix the problem you had, since your problem was in not moving the current pointer (and mallocing an extra node even though push mallocs one).
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Thank you WoodSTokk.

  7. #7
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Quote Originally Posted by algorism View Post
    Why in the world would you post all of the functions separately? That's idiotic. It's just more work for both you and us (if we want to run it). Post full, runnable programs whenever possible.

    And "still not working" is even more stupid. "Not working" how? Obviously Salem's answer would fix the problem you had, since your problem was in not moving the current pointer (and mallocing an extra node even though push mallocs one).
    I posted it separately because I have many other functions in this file, next time i'll post the full code.
    You right I had to explain what is not working again, anyway sorry for my newbie questions and have a good day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-19-2015, 07:23 AM
  2. Printing a linked list
    By popnfresh12321 in forum C Programming
    Replies: 5
    Last Post: 09-22-2012, 05:27 PM
  3. Unable to stop at the end of linked list
    By megablue in forum C Programming
    Replies: 14
    Last Post: 07-16-2004, 11:34 AM
  4. Printing a linked list
    By Jslam9 in forum C++ Programming
    Replies: 5
    Last Post: 12-14-2002, 06:08 PM
  5. linked list doesn't work correctly
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-12-2002, 11:32 AM

Tags for this Thread