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