Thread: Creating a Sorted Linked List, Help Please!

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Creating a Sorted Linked List, Help Please!

    Hello all,

    I'm working on this exercise program where I will have to create a sorted linked list. As each new exercise struct is mallocated and filled with data, the new struct will need to be linked into the list in order. As the data is read in the linked list should be sorted in order by exercise code from smallest to largest.

    This is what I have so far. I'm understanding the logical reasoning behind the link list to a certain extent, but the coding portion is what I'm not understanding. Please Help me out!


    Here is me text input file...


    4 Recumbent_Exercise_Bike E C B S -1 -2 3 20.5 27.5 60.0 22.0 3 5
    50 Machine_Fly S A W W 30 40 0 15 18 1.8 30.0 10 2
    53 Machine_Bicep_Curl S A W W 30 50 0 10 18 3.25 45.0 20 4
    487 Yoga_Sun_Salutation F W M N -1 -2 2 1 3 112.0 1200.0 50 2
    221 Walk E C N N -1 -2 3 30.0 45.0 60.0 180.0 35 5
    488 Yoga_Downward_Dog F W M N -1 -2 2 1 3 20.0 300.0 50 12
    138 Crunch S W N N -1 -2 0 20 40 4.2 240 10 10
    222 Jog E C N N -1 -2 1 .5 1.25 150.5 5000 15 5
    134 Free_Weight_Bicep_Curl S A F W 15 25 0 8 14 4.5 200.0 10 50
    135 Free_Weight_Triceps_Curl S A F W 12 20 0 9 12 5.4 200 10 50
    9 Treadmill E L S S -1 -2 1 50.0 75.5 59.0 25.0 15 10
    222 Jog E C N N -1 -2 3 30.0 45.0 60.0 180.0 35 5



    This is part of my program that I have so far...


    Code:
    int main( int argc, char *argv[] )
    {
            int choice, choice_1 = 0;
            int index, i, j;
            FILE *exercisefile;
            minimum_ex = 12;
    
            //This if-statement will check to see if fopen fails to open the data file
            if( (exercisefile = fopen( "Lab3ExerciseInput.txt" , "r" )) == NULL )
            {
                    printf( "There was an error opening the data file\n" );
                    return;
            }
    
            struct program exercise = {NULL}; //Declaring a struct of programs called exercise
            struct program *head, *new, *temp, *current, *previous; //Declaring pointers
            head = NULL; //beginning of the list
            temp = NULL;
            previous = NULL;
            current = head;
    
            //Starts the Linked List
            head = (struct program *) malloc(sizeof( struct program )); //asking for new chunks of memory for data
            fscanf( exercisefile, "%s" , &head ); //Setting a pointer to point to the first element in the data file
            head->exercise_code = exercise_code;
            head->next = NULL;
    
            //Building a Linked List and sorting it at the same time
            while( !feof( exercisefile ))
            {
                    new = get_space();
                    fscanf( exercisefile, "%s" , &new );
                    new->next = NULL;
    
                    //Test to see if new goes before head
                    if( head->exercise_code > new->exercise_code )
                    {
                            new->next = head;
                            head = new;
                    }
                    else
                    {
                            previous = head;
                            current = head->next;
    
                            While(( current->exercise_code < new->exercise_code ) && ( current != NULL ))/* A counted loop used to find the position */
                            {
                                    //Link it
                                    new->next = current;
                                    previous->next = new;
                            }
                    }//End of the else
            }//End of the while loop

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by larry_2k4 View Post
    This is what I have so far. I'm understanding the logical reasoning behind the link list to a certain extent, but the coding portion is what I'm not understanding.
    I just want you to read that part back to yourself Larry. Anyway, why didn't you include the struct itself? At least that gives the mind something to grab a hold of and hang onto while it delves forward into the rest of your code.

    I am a little dubious of this line:
    Code:
    fscanf( exercisefile, "%s" , &new );
    So what is the plan here, to break this up later? This can not possibly work.

    It will be hard to test the rest of your logic until you can get your individual nodes to exist. Do that before you even think about anything else.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    I trying to use the fscanf function to populate the list with data from the file...

    Here is my struct that I created...

    Code:
    
    enum exmeasurement{
            reps, laps, postures, minutes, other, unknown
    };   //List each type of measurement that is valid
    
    union recent{
            int reps;
            int postures;
            float laps;
            double minutes;
            int other;
            int unknown;
    };  //The value of the most recent measurement recoreded for that particular exercise
    
    union target{
            int reps;
            int postures;
            float laps;
    
            double minutes;
            int other;
            int unknown;
    };  //The value of the target measurement for that particular exercise
    
    
    struct program{
            int exercise_code;      //The integer numeric code that is associated with a particular exercise
            char *exercise_name;    //The name as string with a particular exercise
            char type_of_exercise;  //The single character to hold the letter code for the type of exercise
            char body_part_focus;   //The character to hold the letter code for the body part addressed by the exercise
            char equipment_needed;  //The character to hold the letter code for the type of equipment needed for the exercise
            char equipment_setting; //The character code for the setting of the equipment needed for the exercise
            int recent_setting;     //The integer value for the most recent setting used for that particular exercise
            int target_setting;     //The integer value for the target setting for that particular exercise
            enum exmeasurement exercise_measurement;   //List each type of measurement that is valid
            union recent recent_measurements;  //The value of the most recent measurement recoreded for that particular exercise
            union target target_measurement;   //The value of the target measurement for that particular exercise
            double time_per_measurement; //average time in seconds, and this measurement multiplied by the recent measurement tells how long it takes to do this exercise
            double max_effective_length; //Indicates the maximum length of time that this exercise should be performed during one workout
            float over_max_increment;    //Indicates the time increments over max that actually lead to reduced results
            float over_max_dropoff;      //Indicates the amount of dropoff results per each overmax increment
            double recent_exercise_length; //The value is time per measurement times recent measurement
            struct program *next; //Pointer called next to point to the next node in the list
    };

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by larry_2k4 View Post
    I trying to use the fscanf function to populate the list with data from the file...
    You can't do it that way. You need to parse the line like this:
    Code:
    struct mystruct {
          char this[128];
          int num;
          char that[128];
    }
    struct mystruct eg;
    fscanf(file, "%s %d %s",&eg.this,&eg.num,&eg.that);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by larry_2k4 View Post
    Code:
                            While(( current->exercise_code < new->exercise_code ) && ( current != NULL ))/* A counted loop used to find the position */
    You need to start by writing code that actually compiles. C++ keywords are ALL in entirely lowercase.
    There's also no point in checking current for NULL AFTER you dereference it using ->. If it ever gets to the point where it is NULL, the first part of the && expression will have already crashed your program. You need to swap those halves around.

    Also, read the FAQ about not using feof to control a loop like that, and about not casting malloc.

    That's just a starter, you put some effort in fixing those things and then I might help furthur.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Creating Linked List Using Hashing and Stack
    By m0ntana in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 07:11 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM