Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sorting Problem in Linked List

Posted on 2008-10-03
18
380 Views
Last Modified: 2012-05-05
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;
     }              
  }
}
0
Comment
Question by:dchvic
  • 12
  • 6
18 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22634633
>  for (i=1; i<nodeCount; i++)
It should either be i=0 OR i<=nodeCount
0
 

Author Comment

by:dchvic
ID: 22634732
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 !!
0
 

Author Comment

by:dchvic
ID: 22634736
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 !!
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 22634816
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

0
 

Author Comment

by:dchvic
ID: 22634857
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.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22634890
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.
0
 

Author Comment

by:dchvic
ID: 22635031
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;

      }
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22635087
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?
0
 

Author Comment

by:dchvic
ID: 22635142
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);
}

0
 

Author Comment

by:dchvic
ID: 22635193
I think I got it..I just need to return temp from the sort function !!!
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22635196
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;
}
0
 

Author Comment

by:dchvic
ID: 22635911
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);
}

0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 300 total points
ID: 22636009
You have conflicting type declarations for add()
What is the input file you are using?
Revert
  for (i=1; i<=nodeCount; i++)
 in your sort function to i<nodeCount
0
 

Author Comment

by:dchvic
ID: 22636353
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);
}

0
 

Author Comment

by:dchvic
ID: 22636606
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..
0
 

Author Comment

by:dchvic
ID: 22636610
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..
0
 

Author Comment

by:dchvic
ID: 22637012
Fixed all issues. Everything is fine now :).
Thanks foryour time & help sunnycoder ...
0
 

Author Closing Comment

by:dchvic
ID: 31502782
thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question