Linked List help

This is a discussion on Linked List help within the C Programming forums, part of the General Programming Boards category; I am not getting the correct output from my program. However i know the problem is to do with me ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    Linked List help

    I am not getting the correct output from my program.
    However i know the problem is to do with me not terminating the linked list correctly in the reading function.

    Yet i have no idea how to fix this and have spent the last 12 hours fiddling around with different code, alas no luck.

    If anyone could show me how i need to setup my linked list that would be great!

    Thanks in advance,
    Scupham


    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    
    
    struct filenames
            {
            char measurements_filename[101] ;
            char line_parameters_filename[101];
            };
    
    struct measurements
            {
            double x,y;
            struct measurements *next;
            };
    
    struct line_parameters
            {
            double gradient, constant;
            struct line_parameters *next;
            };
    
    
    
    int main (void)
    {
    struct filenames filenames, *filenames_ptr;
    struct measurements measurements, *measurement_ptr;
    struct line_parameters line_parameters, *line_parameters_ptr;
            
            void get_filenames(struct filenames *);
            void read_measurments(struct filenames*, struct measurements *);
            void calculate_line_parameters(struct measurements*, struct line_parameters * );
            void write_line_parameters(struct filenames, struct line_parameters );
    
    filenames_ptr = &filenames;
    measurement_ptr= &measurements;
    line_parameters_ptr= &line_parameters; 
    
    
    get_filenames(filenames_ptr);
    
    read_measurments(filenames_ptr, measurement_ptr);
    fprintf(stdout, "Measurements Read\n");
    
    calculate_line_parameters(measurement_ptr, line_parameters_ptr);
    fprintf(stdout, "Calculating Parameters\n");
    
    write_line_parameters(filenames, line_parameters);
    fprintf(stdout, "Gradient and Constant written to file.\n");
    return(0);
    }
    
    
    
    
    void get_filenames(struct filenames *filenames_ptr)
    {
    fprintf(stdout, "Enter file name:\n");
    fscanf(stdin, "%s",filenames_ptr->measurements_filename);
    
    fprintf(stdout, "Enter file name to Save to:\n");
    fscanf(stdin, "%s",filenames_ptr->line_parameters_filename);
    return;
    }
    
    
    
    
    void read_measurments(struct filenames *filenames_ptr, struct measurements *measurement_ptr)
    {
    char line[101];
    char *line_ptr;
    int no_values=0;
    struct measurements *current_measurements_ptr=measurement_ptr;
    FILE *input_stream;
    input_stream=fopen(filenames_ptr->measurements_filename, "r");
    
    
    if(input_stream!=NULL)
                    {
                    fprintf(stdout, "File found!\n");
    				line_ptr= line;
                    fgets(line,sizeof(line), input_stream);
                            
                            while (((line_ptr=fgets(line,sizeof(line), input_stream))!=NULL) && ((no_values= sscanf(line,"%lf %lf",
                                                                                    &current_measurements_ptr->x,
                                                                                    &current_measurements_ptr->y))==2))
                            {
                            current_measurements_ptr -> next=(struct measurements*)malloc(sizeof(struct measurements));                
                    
                            if((line_ptr!=NULL) && (no_values!=2))
                            fprintf(stdout, "Error reading line %s \n", line);                
                            current_measurements_ptr -> next= NULL;        
                            
                            }
    }        
    fclose(input_stream);
    return;
    }
                                    
    
    
    
    void calculate_line_parameters(struct measurements *measurements, struct line_parameters *line_parameters_ptr)
    
    {
    struct measurements *measurement_ptr=NULL, *current_measurements_ptr;
    
    double sumx=0, sumy=0, sumxx=0, sumxy=0, n=0;
    
    current_measurements_ptr = measurements;
    
    while(current_measurements_ptr->next != NULL)
            {
    
            sumx  +=  measurement_ptr->x;
            sumy  +=  measurement_ptr->y;
            sumxx += (measurement_ptr->x) * (measurement_ptr->x);
            sumxy += (measurement_ptr->x) * (measurement_ptr->y);
            
            n ++;
            current_measurements_ptr = current_measurements_ptr->next;
            }
    
    line_parameters_ptr->gradient = ((n * sumxy)-(sumx * sumy))/((n * sumxx)-(sumx * sumx));
    line_parameters_ptr->constant = ((sumxy * sumx) - (sumxx * sumy)) / ((sumx * sumx) -(n * sumxx));
    return;
    }
    
    
    
    void write_line_parameters(struct filenames filenames, struct line_parameters line_parameters)
    {
    FILE *output_stream;
    output_stream = fopen(filenames.line_parameters_filename, "w");
    
    fprintf(stdout, "Gradient: %lf \n",line_parameters.gradient);        
    fprintf(stdout, "Constant: %lf \n",line_parameters.constant);
    fprintf(output_stream, "Gradient = %lf \n", line_parameters.gradient);
    fprintf(output_stream, "Constant = %lf \n", line_parameters.constant);
    
    fclose(output_stream);
    return;
    }
    Input Data:
    X Y
    1.0 2.876
    2.6 4.234
    3.7 8.874
    4.5 7.698
    6.0 9.932
    Current Ouput:
    Gradient = -1.#IND00

    Constant = -1.#IND00

  2. #2
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Code:
                            current_measurements_ptr -> next=(struct measurements*)malloc(sizeof(struct measurements));                
                    
                            if((line_ptr!=NULL) && (no_values!=2))
                            fprintf(stdout, "Error reading line &#37;s \n", line);                
                            current_measurements_ptr -> next= NULL;
    Wait... so you allocate then set it to NULL. Memory leak?

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Well that is supposed to tell my linked list where the end is, i.e. NULL = end of list.

    So when doing calculations it gets to a ptr where next=NULL and stops.

  4. #4
    Banned
    Join Date
    Dec 2008
    Posts
    49
    If that is your desired result shouldn't your code be something like

    Option 1:
    Code:
                            current_measurements_ptr -> next=(struct measurements*)malloc(sizeof(struct measurements));                
                    
                            if((line_ptr!=NULL) && (no_values!=2))
                            fprintf(stdout, "Error reading line &#37;s \n", line);                
                            current_measurements_ptr -> next -> next = NULL;
    Option 2:
    Code:
                            current_measurements_ptr -> next=(struct measurements*)malloc(sizeof(struct measurements));                
                            current_measurements_ptr = current_measurements_ptr -> next;
                            if((line_ptr!=NULL) && (no_values!=2))
                            fprintf(stdout, "Error reading line %s \n", line);                
                            current_measurements_ptr -> next = NULL;

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Thanks, tried your suggestions.

    Now the program crashes, here is the debug.
    Seems like the linked list is not finishing when there is no data left.

    Also the data is not being passed on into the calculate_line_parameter

    Im really not sure what is going on now, if you could shed some more light.
    Sorry to be a pain

    See
    Attached Images Attached Images  

  6. #6
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Hmmm wtf happened to line 1? (1.0 2.876) Its not in the list. Lemme look at your code real quick.

  7. #7
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Ok cool, I see

    Code:
     fgets(line,sizeof(line), input_stream); // Delete this line.
                            
                            while (((line_ptr=fgets(line,sizeof(line), input_stream))!=NULL) && ((no_values= sscanf(line,"&#37;lf %lf",
                                                                                    &current_measurements_ptr->x,
                                                                                    &current_measurements_ptr->y))==2))
                            {
                            current_measurements_ptr -> next=(struct measurements*)malloc(sizeof(struct measurements));                
                    
                            if((line_ptr!=NULL) && (no_values!=2))
                            fprintf(stdout, "Error reading line %s \n", line);                
                            current_measurements_ptr -> next= NULL;        
                            *line = 0; // just in case.
                            }

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Hmm now the list does not even start to store values:

    Name:  debug3.jpg
Views: 74
Size:  80.7 KB

    Any other ideas, It is crashing as soon as the program has read the data.

  9. #9
    Banned
    Join Date
    Dec 2008
    Posts
    49
    As it should crash.

    Code:
    char line[101] = "";
    Tada

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Ok added that peice of code to the program, "as it should crash"?

    Any chance of talking on MSN at all? or you prefer over the forum?

    Sorry to be such a pain in the ass.

  11. #11
    Banned
    Join Date
    Dec 2008
    Posts
    49
    You can add me, I am about to go out to lunch. master5001@hotmail.com. I was simply stating that sscanf() is going to always crash when you read random characters. By setting line[] = "" you at least make it an empty string.

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    Thanks ill be around for a wee while,

    I thought the whole point of fgets was to disregard the first line and start storing all the values after?

  13. #13
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Is that what you even wanted to do? If so, go for it. Then you don't need to add those other lines. I simply noticed you do not end up capturing the first line of values.

  14. #14
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    The original data contains headers for the data.

    ie: X and Y

    I was under the impression i could use fget to disregard these and start storing the parameters after this. ie 1, 2,3,4,5

    My main aim for this is to create a linked list that goes from

    1,2,3,4,5 then stops as there is no data left.

    This list then needs to be used in the calculations of the next section

  15. #15
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Ok but I am looking at your debugger display pic you attached. What is the dizzle with no 1.0, 2.876 link in the list?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21