Link to home
Start Free TrialLog in
Avatar of dchvic
dchvic

asked on

Sorting Problem in Linked List

I use the following function to sort a Link List in ascending order based on the char  time(since it's in datetime format, I am just doing a strcmp). But somehow, at the end, there is one element missing in the list.
In my test I had four 4 nodes,after ' sortList' it only has three !!! I will be honest .I didn't entirely write this from scratch.

typedef struct
{
      char Time[MAX_STRING_LEN]; //format is "08/01/2008 12:00:02" //military format time
      float BW;
      int isStartOrEnd;
}Lt;

struct list
{
    Lt data;
    struct list *next;
};

typedef struct list ptr;
void sortList(ptr *element, int nodeCount);
main()
{
...//I am calculating nodecount
sortList(element,nodeCount);
....
}

void sortList(ptr *element,int nodeCount)
{
  ptr *first=0,*second=0,*first_prev=0,*temp=0;
  int i,j;
  temp = element;
  first = element;
  first_prev = element;
  for (i=1; i<nodeCount; i++)
  {
     first = temp;
     first_prev = temp;
     second = first->next;
     for (j=1; j<=(nodeCount-i); j++)
     {
        if((strcmp(first->data.Time,second->data.Time))>0)
        {
                  if (first == temp)  /* If it is an header node */
            {
                        first->next = second->next;
                second->next = first;
                temp = second;
                first = second;
            }
            else
            {
                first->next = second->next;
                second->next = first;
                first_prev->next = second;
                first = second;
            }
        }
      first_prev = first;
        first = first->next;
        second = first->next;
     }              
  }
}
Avatar of sunnycoder
sunnycoder
Flag of India image

>  for (i=1; i<nodeCount; i++)
It should either be i=0 OR i<=nodeCount
Avatar of dchvic
dchvic

ASKER

thanks for the reply. But still have the same problem.. I just tested with just nodes..
before sort i have two nodes..After sort- only one... I am not sure where i throw the other one !!
Avatar of dchvic

ASKER

thanks for the reply. But still have the same problem.. I just tested two* nodes..
before sort i have two nodes..After sort- only one... I am not sure where i throw the other one !!
Your sorting function looks ok .. the problem is some place else ... I tried it some dummy data and it sorts fine

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
typedef struct
{
      char * Time; //format is "08/01/2008 12:00:02" //military format time
      float BW;
      int isStartOrEnd;
}Lt;
 
struct list
{
    Lt data;
    struct list *next;
};
 
typedef struct list ptr;
void sortList(ptr *element, int nodeCount);
main()
{
   ptr * element = NULL, *temp = NULL;
   element = malloc (sizeof(ptr));
   element->data.Time = "rbcd";
   element->next= malloc (sizeof(ptr));
   temp = element->next;
   temp->data.Time = "yyde";
   temp->next= malloc (sizeof(ptr));
   temp = temp->next;
      temp->data.Time = "ccde";
   temp->next= malloc (sizeof(ptr));
      temp = temp->next;
      temp->data.Time = "acde";
   temp->next= NULL;
 
 
 
sortList(element,4);
printf("\n");
}
 
void sortList(ptr *element,int nodeCount)
{
  ptr *first=0,*second=0,*first_prev=0,*temp=0, *temp1;
  int i,j;
  temp = element;
  first = element;
  first_prev = element;
  for (i=1; i<nodeCount; i++)
  {
     first = temp;
     first_prev = temp;
     second = first->next;
     for (j=1; j<=(nodeCount-i); j++)
     {
        if((strcmp(first->data.Time,second->data.Time))>0)
        {
                  if (first == temp)  /* If it is an header node */
            {
                        first->next = second->next;
                second->next = first;
                temp = second;
                first = second;
            }
            else
            {
                first->next = second->next;
                second->next = first;
                first_prev->next = second;
                first = second;
            }
        }
      first_prev = first;
        first = first->next;
        second = first->next;
     }
  }
  for (temp1 = temp; temp1; temp1=temp1->next)
     printf ("%s   ",temp1->data.Time);
}
 
 
Output
acde   ccde   rbcd   yyde

Open in new window

Avatar of dchvic

ASKER

Should I return something  ? I just connected the debugger. At the end of the  loop, the original list 'element' , which is what i print, has only 1 node.
Of course you need to update the beginning of the list in case it has changed. Either return address of first node or use a pointer to pointer to pass the address of the first node and change it within the sort function.
Avatar of dchvic

ASKER

I am still confused :(  am I screwing up during my add ?

main(){
.......
element = add(element,tEBW,tempStartTime,0)
nodeCount++;
element=add(element,tSBW,tempEndTime,1);
nodeCount++;
..............
printf("\n\n**********LIST BEFORE SORTING***********\n\n");
displayList(element);
sortList(element,nodeCount);
printf("\n\n**********LIST AFTER SORTING***********\n\n");
displayList(element);
printf("\n\n**********SORTING DONE*****************\n\n");

}

ptr *add(ptr *element,float BW,char *Time, int isStartOrEnd )
      {
          ptr *temp;
          temp = element;
          if(element==NULL)
            {
                  element = malloc(sizeof(struct list));
                  temp = element;
            }
          else
            {
                  //Go to the Last Node
                  while(temp -> next!= NULL)
                  temp = temp -> next;
                  temp -> next=malloc(sizeof(struct list));//Allocate memory and create node
                temp = temp -> next;
            }
            memset(temp ->data.Time,0,MAX_STRING_LEN);
            memcpy(temp ->data.Time,Time,strlen(Time)+1);//Populate Time      
            temp ->data.BW = BW;//
            temp->data.isStartOrEnd = isStartOrEnd;
            temp -> next = NULL;
            return element;

      }
Would it be possible to post the entire code ... that way I can run it and find the problem. Also, what exactly is the problem you are facing?
Did you return the start address from sort function?
Avatar of dchvic

ASKER

i/p csv file will have different columns...I am selecting the specific columns like start_time,end_time and bitrate..
Each node will have 3 values ..time,bitrate and a flag (which i later use to some calculations)....
I add nodes as i read the csv file.. i discard the column(the first line)..and all junk values...

i sort them..
i print the new values into a csv ( in the fly i do some calculations)

but the problem happens after sort !!!




#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define MAX_LINE_LENGTH  1024
#define MAX_STRING_LEN       30

typedef struct
{
      char Time[MAX_STRING_LEN]; //This could be start time or endtime
      float BW;//BW of the start or endtime
      int isStartOrEnd;//Will tell us whether StartTime or EndTime. 0 = startime,1=endtime
}VOD;

struct list
{
    VOD data;/*DATA PART OF THE LINK LIST*/
    struct list *next;/*POINTER TO THE NEXT NODE*/
};
typedef struct list ptr;
ptr *add(ptr *q,float BW,char *Time);
void sortList(ptr *element, int nodeCount);
void displayList(ptr *element);
void createFile(ptr *element);
int main(int argc, char * argv[])
{            
        char line[MAX_LINE_LENGTH] = { 0 };
        ptr *element = NULL;
            char tempStartTime[MAX_STRING_LEN];
            char tempEndTime[MAX_STRING_LEN];
            float tempStartTimeBitRate;
            float tempEndTimeBitRate;
            int colNum= 0,nodeCount=0,s_invalid=0,e_invalid=0;
            int StartTime,EndTime,BitRate;
            //FILE *csvOut = fopen("csv_output.csv","w+");
        FILE *csvIn = fopen("small.csv", "r");
            printf("\n Enter the START_TIME Column row number ");
            scanf("%d",&StartTime);
            printf("\n Enter the END_TIME Column row number ");
            scanf("%d",&EndTime);
            printf("\n Enter the BITRATE  Column row number ");
            scanf("%d",&BitRate);      
        if (csvIn) {
                        char *str = 0; int first_line=1;
                while (fgets(line, MAX_LINE_LENGTH, csvIn)) {
                                    str = strtok(&line[0], ","); // , is delimiter
                        while (str) {
                                ++colNum;
                                                if (colNum==BitRate){                                                      
                                                      puts(str);                                                      
                                                      sscanf(str,"%f",&tempStartTimeBitRate);
                                                      sscanf(str,"%f",&tempEndTimeBitRate);
                                                }                                          
                                                if (colNum==StartTime) {
                                                      puts(str);
                                                      if(15<strlen(str)){
                                                            memcpy(tempStartTime,str,strlen(str)+1);
                                                            s_invalid=0;//strcpy(tempStartTime,str);
                                                      }
                                                      else
                                                            s_invalid=1;
                                                }
                                                if (colNum==EndTime){
                                                      puts(str);
                                                      if(15<strlen(str)){
                                                            memcpy(tempEndTime,str,strlen(str)+1);
                                                            e_invalid=0;//strcpy(tempEndTime,str);
                                                      }
                                                      else
                                                            e_invalid=1;
                                                }                                          
                                str = strtok(NULL, ",");
                        }
                                    if(first_line==0){
                                          if(!s_invalid){//only valid data should be added
                                                element = add(element,tempStartTimeBitRate,tempStartTime,0);
                                                nodeCount++;
                                                s_invalid=1;
                                          }
                                          if(!e_invalid){
                                                element=add(element,tempEndTimeBitRate,tempEndTime,1);
                                                nodeCount++;
                                                e_invalid=1;
                                          }
                                    }
                                    memset(tempStartTime,0,MAX_STRING_LEN);
                                    memset(tempEndTime,0,MAX_STRING_LEN);
                                    
                                    if(first_line==1)
                                          (first_line=0);
                                    colNum=0;                      
                }
                fclose (csvIn);                        
        }
            printf("\n\n**********LIST BEFORE SORTING***********\n\n");
            displayList(element);
          sortList(element,nodeCount);
            printf("\n\n**********LIST AFTER SORTING***********\n\n");
            displayList(element);
            printf("\n\n**********SORTING DONE*****************\n\n");
            createFile(element);
}

//This function add's nodes to the Linked List
ptr *add(ptr *element,float BW,char *Time, int isStartOrEnd )
      {
          ptr *temp;
          temp = element;
          if(element==NULL)
            {
                  element = malloc(sizeof(struct list));
                  temp = element;
            }
          else
            {
                  //Go to the Last Node
                  while(temp -> next!= NULL)
                  temp = temp -> next;
                  temp -> next=malloc(sizeof(struct list));//Allocate memory and create node
                temp = temp -> next;
            }
            memset(temp ->data.Time,0,MAX_STRING_LEN);
            memcpy(temp ->data.Time,Time,strlen(Time)+1);//Populate Time      
            temp ->data.BW = BW;//Populate Index
            temp->data.isStartOrEnd = isStartOrEnd;
            temp -> next = NULL;
            return element;

      }

//This Function Sorts The Linked List
void sortList(ptr *element,int nodeCount)
{
  ptr *first=0,*second=0,*first_prev=0,*temp=0;
  int i,j;
  temp = element;
  first = element;
  first_prev = element;
  for (i=1; i<=nodeCount; i++)
  {
     first = temp;
     first_prev = temp;
     second = first->next;
     for (j=1; j<=(nodeCount-i); j++)
     {
        if((strcmp(first->data.Time,second->data.Time))>0)
        {
                  if (first == temp)  /* If it is an header node */
            {
                        first->next = second->next;
                second->next = first;
                temp = second;
                first = second;
            }
            else
            {
                first->next = second->next;
                second->next = first;
                first_prev->next = second;
                first = second;
            }
        }
            first_prev = first;
        first = first->next;
        second = first->next;
     }              
  }
}
// This function displays the Linked List
void displayList(ptr *element)
{
      while(element!=NULL){
            //if((0!=strlen(element->data.Time))&&(15<strlen(element->data.Time))){
                  puts(element->data.Time);
                  puts("\t");
                  printf("%f",element->data.BW);
                  puts("\n");
            //}
            element=element->next;
      }
}
      
//This function creates the final csv file
void createFile(ptr *element)
{
      float BW_Count=0;
      int ct=0;
      char x[50];
      FILE *csvOut = fopen("csv_output.csv","w+");       
      if(csvOut){
            fputs("TIMESTAMP,BITRATE\n",csvOut);
            while(element!=NULL){
                  if((0!=strlen(element->data.Time))&&(15<strlen(element->data.Time))){
                        fputs(element->data.Time,csvOut);
                        fputs(",",csvOut);
                        if (ct==0){
                              BW_Count = element->data.BW;
                              ct=1;
                        }else {
                              if(!element->data.isStartOrEnd)//start time
                                    BW_Count = BW_Count+element->data.BW;
                              else
                                    BW_Count = BW_Count-element->data.BW;
                        }
                        fprintf(csvOut,"%.4f",BW_Count);
                        fputs("\n",csvOut);
                        printf("\n");
                        puts(element->data.Time);
                        printf("\n%.4f",BW_Count);                
                  }
            element=element->next;
            }
      }
    fclose(csvOut);
}

Avatar of dchvic

ASKER

I think I got it..I just need to return temp from the sort function !!!
ptr * sortList(ptr *element,int nodeCount)
{
  ptr *first=0,*second=0,*first_prev=0,*temp=0;
  int i,j;
  temp = element;
  first = element;
  first_prev = element;
  for (i=1; i<=nodeCount; i++)
  {
     first = temp;
     first_prev = temp;
     second = first->next;
     for (j=1; j<=(nodeCount-i); j++)
     {
        if((strcmp(first->data.Time,second->data.Time))>0)
        {
                  if (first == temp)  /* If it is an header node */
            {
                        first->next = second->next;
                second->next = first;
                temp = second;
                first = second;
            }
            else
            {
                first->next = second->next;
                second->next = first;
                first_prev->next = second;
                first = second;
            }
        }
            first_prev = first;
        first = first->next;
        second = first->next;
     }              
  }
   return temp;
}
Avatar of dchvic

ASKER

Thanks again.. It fixed the sorting problem.. But when I was testing, with huge files..
I get "runtime check failure #2 - stack around the variable 'tempStartTime' was corrupted"..I am writing and clearing 'tempStartTime' properly...is it because of it's a huge file ?..Every thing happens during the CreateFile function, where, I think 'tempStartTime' has nothing to do with

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define MAX_LINE_LENGTH  1024
#define MAX_STRING_LEN       30

typedef struct
{
      char Time[MAX_STRING_LEN]; //This could be start time or endtime
      float BW;//BW of the start or endtime
      int isStartOrEnd;//Will tell us whether StartTime or EndTime. 0 = startime,1=endtime
}VOD;

struct list
{
    VOD data;/*DATA PART OF THE LINK LIST*/
    struct list *next;/*POINTER TO THE NEXT NODE*/
};
typedef struct list ptr;
ptr *add(ptr *q,float BW,char *Time);
ptr *sortList(ptr *element, int nodeCount);
void displayList(ptr *element);
void createFile(ptr *element);
int main(int argc, char * argv[])
{            
        char line[MAX_LINE_LENGTH] = { 0 };
        ptr *element = NULL;
            ptr *final = NULL;
            char tempStartTime[MAX_STRING_LEN];
            char tempEndTime[MAX_STRING_LEN];
            float tempStartTimeBitRate;
            float tempEndTimeBitRate;
            int colNum= 0,nodeCount=0,s_invalid=0,e_invalid=0;
            int StartTime,EndTime,BitRate;
            //FILE *csvOut = fopen("csv_output.csv","w+");
        FILE *csvIn = fopen("test.csv", "r");
            printf("\n Enter the START_TIME Column row number ");
            scanf("%d",&StartTime);
            printf("\n Enter the END_TIME Column row number ");
            scanf("%d",&EndTime);
            printf("\n Enter the BITRATE  Column row number ");
            scanf("%d",&BitRate);
            memset(tempStartTime,0,MAX_STRING_LEN);
            memset(tempEndTime,0,MAX_STRING_LEN);
        if (csvIn) {
                        char *str = 0; int first_line=1;
                while (fgets(line, MAX_LINE_LENGTH, csvIn)) {
                                    str = strtok(&line[0], ","); // , is delimiter
                        while (str) {
                                ++colNum;
                                                if (colNum==BitRate){                                                      
                                                      //puts(str);                                                      
                                                      sscanf(str,"%f",&tempStartTimeBitRate);
                                                      sscanf(str,"%f",&tempEndTimeBitRate);
                                                }                                          
                                                if (colNum==StartTime) {
                                                      //puts(str);
                                                      if(15<strlen(str)){
                                                            memcpy(tempStartTime,str,strlen(str)+1);
                                                            s_invalid=0;//strcpy(tempStartTime,str);
                                                      }
                                                      else
                                                            s_invalid=1;
                                                }
                                                if (colNum==EndTime){
                                                      //puts(str);
                                                      if(15<strlen(str)){
                                                            memcpy(tempEndTime,str,strlen(str)+1);
                                                            e_invalid=0;//strcpy(tempEndTime,str);
                                                      }
                                                      else
                                                            e_invalid=1;
                                                }                                          
                                str = strtok(NULL, ",");
                        }
                                    if(first_line==0){
                                          if((!s_invalid)&&(!tempStartTime)){//only valid data should be added
                                                element = add(element,tempStartTimeBitRate,tempStartTime,0);
                                                nodeCount++;
                                                s_invalid=1;
                                          }
                                          if((!e_invalid)&&(!tempEndTime)){
                                                element=add(element,tempEndTimeBitRate,tempEndTime,1);
                                                nodeCount++;
                                                e_invalid=1;
                                          }
                                    }
                                    memset(tempStartTime,0,MAX_STRING_LEN);
                                    memset(tempEndTime,0,MAX_STRING_LEN);
                                    
                                    if(first_line==1)
                                          (first_line=0);
                                    colNum=0;                      
                }
                fclose (csvIn);                        
        }
            printf("\n\n**********LIST BEFORE SORTING***********\n\n");
            //displayList(element);
          final = sortList(element,nodeCount);
            printf("\n\n**********LIST AFTER SORTING***********\n\n");
            //displayList(final);
            printf("\n\n**********SORTING DONE*****************\n\n");
            createFile(final);
}

//This function add's nodes to the Linked List
ptr *add(ptr *element,float BW,char *Time, int isStartOrEnd )
      {
          ptr *temp;
          temp = element;
          if(element==NULL)
            {
                  element = malloc(sizeof(struct list));
                  temp = element;
            }
          else
            {
                  //Go to the Last Node
                  while(temp -> next!= NULL)
                  temp = temp -> next;
                  temp -> next=malloc(sizeof(struct list));//Allocate memory and create node
                temp = temp -> next;
            }
            memset(temp ->data.Time,0,MAX_STRING_LEN);
            memcpy(temp ->data.Time,Time,strlen(Time)+1);//Populate Time      
            if(sizeof(temp ->data.Time)>MAX_STRING_LEN)
                  printf("\n RAN OUT OF BOUNDS !!!!!!!");
            temp ->data.BW = BW;//Populate Index
            temp->data.isStartOrEnd = isStartOrEnd;
            temp -> next = NULL;
            return element;

      }

//This Function Sorts The Linked List
ptr *sortList(ptr *element,int nodeCount)
{
  ptr *first=0,*second=0,*first_prev=0,*temp=0;
  int i,j;
  temp = element;
  first = element;
  first_prev = element;
  for (i=1; i<=nodeCount; i++)
  {
     first = temp;
     first_prev = temp;
     second = first->next;
     for (j=1; j<=(nodeCount-i); j++)
     {
        if((strcmp(first->data.Time,second->data.Time))>0)
        {
                  if (first == temp)  /* If it is an header node */
            {
                        first->next = second->next;
                second->next = first;
                temp = second;
                first = second;
            }
            else
            {
                first->next = second->next;
                second->next = first;
                first_prev->next = second;
                first = second;
            }
        }
            first_prev = first;
        first = first->next;
        second = first->next;
     }              
  }
  return temp;
}
// This function displays the Linked List
void displayList(ptr *element)
{
      while(element!=NULL){
            //if((0!=strlen(element->data.Time))&&(15<strlen(element->data.Time))){
                  puts(element->data.Time);
                  puts("\t");
                  printf("%f",element->data.BW);
                  puts("\n");
            //}
            element=element->next;
      }
}
      
//This function creates the final csv file
void createFile(ptr *final)
{
      double BW_Count=0;
      double prev_total=0;
      int ct=0;
      FILE *csvOut = fopen("csv_output.csv","w+");       
      if(csvOut){
            fputs("TIMESTAMP,BITRATE\n",csvOut);
            while(final!=NULL){
                  if((0!=strlen(final->data.Time))&&(15<strlen(final->data.Time))){
                        if(MAX_STRING_LEN>(strlen(final->data.Time))){
                        fputs(final->data.Time,csvOut);
                        fputs("\n",csvOut);
                        }
                        /*fputs(",",csvOut);
                        if (ct==0){
                              prev_total = element->data.BW;
                              ct=1;
                        }else {
                              if(!element->data.isStartOrEnd)//start time
                                    prev_total= prev_total+element->data.BW;
                              else
                                    prev_total = prev_total-element->data.BW;
                        }
                        BW_Count=prev_total;
                        //printf("\n%lf",BW_Count);
                        fprintf(csvOut,"%lf\n",BW_Count);*/
                        //fputs("\n",csvOut);
                        //printf("\n");
                        //puts(element->data.Time);
                        //printf("\n%lf",BW_Count);                
                  }
            final=final->next;
            }
      }
    fclose(csvOut);
}

ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dchvic

ASKER

I  fixed the declarations for add(). I am using a  csv file as input
In the create file function, I am not even entering the while loop because element is null....

anything wrong here ?


          element = sortList(element,nodeCount);
            printf("\n\n**********LIST AFTER SORTING***********\n\n");
            //displayList(final);
            printf("\n\n**********SORTING DONE*****************\n\n");
            createFile(element);

because , in the debugger, after sortList function, element is nulll..it doesnt get the temp value from the return...I am already losing my mind :( !!! ....I am sure i am committing  a silly mistake ..just can't figure out.....

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define MAX_LINE_LENGTH  1024
#define MAX_STRING_LEN       30

typedef struct
{
      char Time[MAX_STRING_LEN]; //This could be start time or endtime
      float BW;//BW of the start or endtime
      int isStartOrEnd;//Will tell us whether StartTime or EndTime. 0 = startime,1=endtime
}VOD;

struct list
{
    VOD data;/*DATA PART OF THE LINK LIST*/
    struct list *next;/*POINTER TO THE NEXT NODE*/
};
typedef struct list ptr;
ptr *add(ptr *element,float BW,char *Time, int isStartOrEnd );
ptr *sortList(ptr *element, int nodeCount);
void displayList(ptr *element);
void createFile(ptr *element);
int main(int argc, char * argv[])
{            
        char line[MAX_LINE_LENGTH] = { 0 };
        ptr *element = NULL;
            char tempStartTime[MAX_STRING_LEN];
            char tempEndTime[MAX_STRING_LEN];
            float tempStartTimeBitRate;
            float tempEndTimeBitRate;
            int colNum= 0,nodeCount=0,s_invalid=0,e_invalid=0;
            int StartTime,EndTime,BitRate;
            //FILE *csvOut = fopen("csv_output.csv","w+");
        FILE *csvIn = fopen("test1.csv", "r");
            printf("\n Enter the START_TIME Column row number ");
            scanf("%d",&StartTime);
            printf("\n Enter the END_TIME Column row number ");
            scanf("%d",&EndTime);
            printf("\n Enter the BITRATE  Column row number ");
            scanf("%d",&BitRate);
            memset(tempStartTime,0,MAX_STRING_LEN);
            memset(tempEndTime,0,MAX_STRING_LEN);
        if (csvIn) {
                        char *str = 0; int first_line=1;
                while (fgets(line, MAX_LINE_LENGTH, csvIn)) {
                                    str = strtok(&line[0], ","); // , is delimiter
                        while (str) {
                                ++colNum;
                                                if (colNum==BitRate){                                                      
                                                      //puts(str);                                                      
                                                      sscanf(str,"%f",&tempStartTimeBitRate);
                                                      sscanf(str,"%f",&tempEndTimeBitRate);
                                                }                                          
                                                if (colNum==StartTime) {
                                                      //puts(str);
                                                      if(15<strlen(str)){
                                                            memcpy(tempStartTime,str,strlen(str)+1);
                                                            s_invalid=0;//strcpy(tempStartTime,str);
                                                      }
                                                      else
                                                            s_invalid=1;
                                                }
                                                if (colNum==EndTime){
                                                      //puts(str);
                                                      if(15<strlen(str)){
                                                            memcpy(tempEndTime,str,strlen(str)+1);
                                                            e_invalid=0;//strcpy(tempEndTime,str);
                                                      }
                                                      else
                                                            e_invalid=1;
                                                }                                          
                                str = strtok(NULL, ",");
                        }
                                    if(first_line==0){
                                          if((!s_invalid)&&(!tempStartTime)){//only valid data should be added
                                                element = add(element,tempStartTimeBitRate,tempStartTime,0);
                                                nodeCount++;
                                                s_invalid=1;
                                          }
                                          if((!e_invalid)&&(!tempEndTime)){
                                                element=add(element,tempEndTimeBitRate,tempEndTime,1);
                                                nodeCount++;
                                                e_invalid=1;
                                          }
                                    }
                                    memset(tempStartTime,0,MAX_STRING_LEN);
                                    memset(tempEndTime,0,MAX_STRING_LEN);
                                    
                                    if(first_line==1)
                                          (first_line=0);
                                    colNum=0;                      
                }
                fclose (csvIn);                        
        }
            printf("\n\n**********LIST BEFORE SORTING***********\n\n");
            //displayList(element);
          element = sortList(element,nodeCount);
            printf("\n\n**********LIST AFTER SORTING***********\n\n");
            //displayList(final);
            printf("\n\n**********SORTING DONE*****************\n\n");
            createFile(element);
}

//This function add's nodes to the Linked List
ptr *add(ptr *element,float BW,char *Time, int isStartOrEnd )
      {
          ptr *temp;
          temp = element;
          if(element==NULL)
            {
                  element = malloc(sizeof(struct list));
                  temp = element;
            }
          else
            {
                  //Go to the Last Node
                  while(temp -> next!= NULL)
                  temp = temp -> next;
                  temp -> next=malloc(sizeof(struct list));//Allocate memory and create node
                temp = temp -> next;
            }
            memset(temp ->data.Time,0,MAX_STRING_LEN);
            memcpy(temp ->data.Time,Time,strlen(Time)+1);//Populate Time      
            if(sizeof(temp ->data.Time)>MAX_STRING_LEN)
                  printf("\n RAN OUT OF BOUNDS !!!!!!!");
            temp ->data.BW = BW;//Populate Index
            temp->data.isStartOrEnd = isStartOrEnd;
            temp -> next = NULL;
            return element;

      }

//This Function Sorts The Linked List
ptr *sortList(ptr *element,int nodeCount)
{
  ptr *first=0,*second=0,*first_prev=0,*temp=0;
  int i,j;
  temp = element;
  first = element;
  first_prev = element;
  for (i=1; i<nodeCount; i++)
  {
     first = temp;
     first_prev = temp;
     second = first->next;
     for (j=1; j<=(nodeCount-i); j++)
     {
        if((strcmp(first->data.Time,second->data.Time))>0)
        {
                  if (first == temp)  /* If it is an header node */
            {
                        first->next = second->next;
                second->next = first;
                temp = second;
                first = second;
            }
            else
            {
                first->next = second->next;
                second->next = first;
                first_prev->next = second;
                first = second;
            }
        }
            first_prev = first;
        first = first->next;
        second = first->next;
     }              
  }
  return temp;
}
// This function displays the Linked List
void displayList(ptr *element)
{
      while(element!=NULL){
            //if((0!=strlen(element->data.Time))&&(15<strlen(element->data.Time))){
                  puts(element->data.Time);
                  puts("\t");
                  printf("%f",element->data.BW);
                  puts("\n");
            //}
            element=element->next;
      }
}
      
//This function creates the final csv file
void createFile(ptr *element)
{
      double BW_Count=0;
      double prev_total=0;
      int ct=0;
      FILE *csvOut = fopen("csv_output.csv","w+");       
      if(csvOut){
            fputs("TIMESTAMP,BITRATE\n",csvOut);
            while(element!=NULL){
                  if((0!=strlen(element->data.Time))&&(15<strlen(element->data.Time))){
                        if(MAX_STRING_LEN>(strlen(element->data.Time))){
                        fputs(element->data.Time,csvOut);
                        //puts("\n",csvOut);
                        //}
                        fputs(",",csvOut);
                        if (ct==0){
                              prev_total = element->data.BW;
                              ct=1;
                        }else {
                              if(!element->data.isStartOrEnd)//start time
                                    prev_total= prev_total+element->data.BW;
                              else
                                    prev_total = prev_total-element->data.BW;
                        }
                        BW_Count=prev_total;
                        //printf("\n%lf",BW_Count);
                        fprintf(csvOut,"%lf\n",BW_Count);
                        fputs("\n",csvOut);
                        //printf("\n");
                        //puts(element->data.Time);
                        //printf("\n%lf",BW_Count);                
                        }
            }
            element=element->next;
            }
      }
    fclose(csvOut);
}

Avatar of dchvic

ASKER

I shoudn't checked tempStartTime in this line >> if((!s_invalid)&&(!tempStartTime))
That was the problem !!! I changed it and currently running my code for a huge file..
Avatar of dchvic

ASKER

I shoudn't have checked tempStartTime in this line >> if((!s_invalid)&&(!tempStartTime))
That was the problem !!! I changed it and currently running my code for a huge file..
Avatar of dchvic

ASKER

Fixed all issues. Everything is fine now :).
Thanks foryour time & help sunnycoder ...
Avatar of dchvic

ASKER

thanks