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,*te mp=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.Tim e,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;
}
}
}
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_
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.Tim
{
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;
}
}
}
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 !!
before sort i have two nodes..After sort- only one... I am not sure where i throw the other one !!
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 !!
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
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.
ASKER
I am still confused :( am I screwing up during my add ?
main(){
.......
element = add(element,tEBW,tempStart Time,0)
nodeCount++;
element=add(element,tSBW,t empEndTime ,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**********SORT ING 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_L EN);
memcpy(temp ->data.Time,Time,strlen(Ti me)+1);//P opulate Time
temp ->data.BW = BW;//
temp->data.isStartOrEnd = isStartOrEnd;
temp -> next = NULL;
return element;
}
main(){
.......
element = add(element,tEBW,tempStart
nodeCount++;
element=add(element,tSBW,t
nodeCount++;
..............
printf("\n\n**********LIST
displayList(element);
sortList(element,nodeCount
printf("\n\n**********LIST
displayList(element);
printf("\n\n**********SORT
}
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_L
memcpy(temp ->data.Time,Time,strlen(Ti
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?
Did you return the start address from sort function?
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_L EN];
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",&tempStart TimeBitRat e);
sscanf(str,"%f",&tempEndTi meBitRate) ;
}
if (colNum==StartTime) {
puts(str);
if(15<strlen(str)){
memcpy(tempStartTime,str,s trlen(str) +1);
s_invalid=0;//strcpy(tempS tartTime,s tr);
}
else
s_invalid=1;
}
if (colNum==EndTime){
puts(str);
if(15<strlen(str)){
memcpy(tempEndTime,str,str len(str)+1 );
e_invalid=0;//strcpy(tempE ndTime,str );
}
else
e_invalid=1;
}
str = strtok(NULL, ",");
}
if(first_line==0){
if(!s_invalid){//only valid data should be added
element = add(element,tempStartTimeB itRate,tem pStartTime ,0);
nodeCount++;
s_invalid=1;
}
if(!e_invalid){
element=add(element,tempEn dTimeBitRa te,tempEnd Time,1);
nodeCount++;
e_invalid=1;
}
}
memset(tempStartTime,0,MAX _STRING_LE N);
memset(tempEndTime,0,MAX_S TRING_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**********SORT ING 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_L EN);
memcpy(temp ->data.Time,Time,strlen(Ti me)+1);//P opulate 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,*te mp=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.Tim e,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->d ata.Time)) &&(15<strl en(element ->data.Tim e))){
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->dat a.Time))&& (15<strlen (element-> data.Time) )){
fputs(element->data.Time,c svOut);
fputs(",",csvOut);
if (ct==0){
BW_Count = element->data.BW;
ct=1;
}else {
if(!element->data.isStartO rEnd)//sta rt time
BW_Count = BW_Count+element->data.BW;
else
BW_Count = BW_Count-element->data.BW;
}
fprintf(csvOut,"%.4f",BW_C ount);
fputs("\n",csvOut);
printf("\n");
puts(element->data.Time);
printf("\n%.4f",BW_Count);
}
element=element->next;
}
}
fclose(csvOut);
}
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_L
char tempEndTime[MAX_STRING_LEN
float tempStartTimeBitRate;
float tempEndTimeBitRate;
int colNum= 0,nodeCount=0,s_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",&tempStart
sscanf(str,"%f",&tempEndTi
}
if (colNum==StartTime) {
puts(str);
if(15<strlen(str)){
memcpy(tempStartTime,str,s
s_invalid=0;//strcpy(tempS
}
else
s_invalid=1;
}
if (colNum==EndTime){
puts(str);
if(15<strlen(str)){
memcpy(tempEndTime,str,str
e_invalid=0;//strcpy(tempE
}
else
e_invalid=1;
}
str = strtok(NULL, ",");
}
if(first_line==0){
if(!s_invalid){//only valid data should be added
element = add(element,tempStartTimeB
nodeCount++;
s_invalid=1;
}
if(!e_invalid){
element=add(element,tempEn
nodeCount++;
e_invalid=1;
}
}
memset(tempStartTime,0,MAX
memset(tempEndTime,0,MAX_S
if(first_line==1)
(first_line=0);
colNum=0;
}
fclose (csvIn);
}
printf("\n\n**********LIST
displayList(element);
sortList(element,nodeCount
printf("\n\n**********LIST
displayList(element);
printf("\n\n**********SORT
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_L
memcpy(temp ->data.Time,Time,strlen(Ti
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_
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.Tim
{
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->d
puts(element->data.Time);
puts("\t");
printf("%f",element->data.
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
while(element!=NULL){
if((0!=strlen(element->dat
fputs(element->data.Time,c
fputs(",",csvOut);
if (ct==0){
BW_Count = element->data.BW;
ct=1;
}else {
if(!element->data.isStartO
BW_Count = BW_Count+element->data.BW;
else
BW_Count = BW_Count-element->data.BW;
}
fprintf(csvOut,"%.4f",BW_C
fputs("\n",csvOut);
printf("\n");
puts(element->data.Time);
printf("\n%.4f",BW_Count);
}
element=element->next;
}
}
fclose(csvOut);
}
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,*te mp=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.Tim e,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;
}
{
ptr *first=0,*second=0,*first_
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.Tim
{
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;
}
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_L EN];
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_LE N);
memset(tempEndTime,0,MAX_S TRING_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",&tempStart TimeBitRat e);
sscanf(str,"%f",&tempEndTi meBitRate) ;
}
if (colNum==StartTime) {
//puts(str);
if(15<strlen(str)){
memcpy(tempStartTime,str,s trlen(str) +1);
s_invalid=0;//strcpy(tempS tartTime,s tr);
}
else
s_invalid=1;
}
if (colNum==EndTime){
//puts(str);
if(15<strlen(str)){
memcpy(tempEndTime,str,str len(str)+1 );
e_invalid=0;//strcpy(tempE ndTime,str );
}
else
e_invalid=1;
}
str = strtok(NULL, ",");
}
if(first_line==0){
if((!s_invalid)&&(!tempSta rtTime)){/ /only valid data should be added
element = add(element,tempStartTimeB itRate,tem pStartTime ,0);
nodeCount++;
s_invalid=1;
}
if((!e_invalid)&&(!tempEnd Time)){
element=add(element,tempEn dTimeBitRa te,tempEnd Time,1);
nodeCount++;
e_invalid=1;
}
}
memset(tempStartTime,0,MAX _STRING_LE N);
memset(tempEndTime,0,MAX_S TRING_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**********SORT ING 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_L EN);
memcpy(temp ->data.Time,Time,strlen(Ti me)+1);//P opulate Time
if(sizeof(temp ->data.Time)>MAX_STRING_LE N)
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,*te mp=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.Tim e,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->d ata.Time)) &&(15<strl en(element ->data.Tim e))){
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))&&(1 5<strlen(f inal->data .Time))){
if(MAX_STRING_LEN>(strlen( final->dat a.Time))){
fputs(final->data.Time,csv Out);
fputs("\n",csvOut);
}
/*fputs(",",csvOut);
if (ct==0){
prev_total = element->data.BW;
ct=1;
}else {
if(!element->data.isStartO rEnd)//sta rt time
prev_total= prev_total+element->data.B W;
else
prev_total = prev_total-element->data.B W;
}
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);
}
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_L
char tempEndTime[MAX_STRING_LEN
float tempStartTimeBitRate;
float tempEndTimeBitRate;
int colNum= 0,nodeCount=0,s_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
memset(tempEndTime,0,MAX_S
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",&tempStart
sscanf(str,"%f",&tempEndTi
}
if (colNum==StartTime) {
//puts(str);
if(15<strlen(str)){
memcpy(tempStartTime,str,s
s_invalid=0;//strcpy(tempS
}
else
s_invalid=1;
}
if (colNum==EndTime){
//puts(str);
if(15<strlen(str)){
memcpy(tempEndTime,str,str
e_invalid=0;//strcpy(tempE
}
else
e_invalid=1;
}
str = strtok(NULL, ",");
}
if(first_line==0){
if((!s_invalid)&&(!tempSta
element = add(element,tempStartTimeB
nodeCount++;
s_invalid=1;
}
if((!e_invalid)&&(!tempEnd
element=add(element,tempEn
nodeCount++;
e_invalid=1;
}
}
memset(tempStartTime,0,MAX
memset(tempEndTime,0,MAX_S
if(first_line==1)
(first_line=0);
colNum=0;
}
fclose (csvIn);
}
printf("\n\n**********LIST
//displayList(element);
final = sortList(element,nodeCount
printf("\n\n**********LIST
//displayList(final);
printf("\n\n**********SORT
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_L
memcpy(temp ->data.Time,Time,strlen(Ti
if(sizeof(temp ->data.Time)>MAX_STRING_LE
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_
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.Tim
{
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->d
puts(element->data.Time);
puts("\t");
printf("%f",element->data.
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
while(final!=NULL){
if((0!=strlen(final->data.
if(MAX_STRING_LEN>(strlen(
fputs(final->data.Time,csv
fputs("\n",csvOut);
}
/*fputs(",",csvOut);
if (ct==0){
prev_total = element->data.BW;
ct=1;
}else {
if(!element->data.isStartO
prev_total= prev_total+element->data.B
else
prev_total = prev_total-element->data.B
}
BW_Count=prev_total;
//printf("\n%lf",BW_Count)
fprintf(csvOut,"%lf\n",BW_
//fputs("\n",csvOut);
//printf("\n");
//puts(element->data.Time)
//printf("\n%lf",BW_Count)
}
final=final->next;
}
}
fclose(csvOut);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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**********SORT ING 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_L EN];
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_LE N);
memset(tempEndTime,0,MAX_S TRING_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",&tempStart TimeBitRat e);
sscanf(str,"%f",&tempEndTi meBitRate) ;
}
if (colNum==StartTime) {
//puts(str);
if(15<strlen(str)){
memcpy(tempStartTime,str,s trlen(str) +1);
s_invalid=0;//strcpy(tempS tartTime,s tr);
}
else
s_invalid=1;
}
if (colNum==EndTime){
//puts(str);
if(15<strlen(str)){
memcpy(tempEndTime,str,str len(str)+1 );
e_invalid=0;//strcpy(tempE ndTime,str );
}
else
e_invalid=1;
}
str = strtok(NULL, ",");
}
if(first_line==0){
if((!s_invalid)&&(!tempSta rtTime)){/ /only valid data should be added
element = add(element,tempStartTimeB itRate,tem pStartTime ,0);
nodeCount++;
s_invalid=1;
}
if((!e_invalid)&&(!tempEnd Time)){
element=add(element,tempEn dTimeBitRa te,tempEnd Time,1);
nodeCount++;
e_invalid=1;
}
}
memset(tempStartTime,0,MAX _STRING_LE N);
memset(tempEndTime,0,MAX_S TRING_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**********SORT ING 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_L EN);
memcpy(temp ->data.Time,Time,strlen(Ti me)+1);//P opulate Time
if(sizeof(temp ->data.Time)>MAX_STRING_LE N)
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,*te mp=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.Tim e,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->d ata.Time)) &&(15<strl en(element ->data.Tim e))){
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->dat a.Time))&& (15<strlen (element-> data.Time) )){
if(MAX_STRING_LEN>(strlen( element->d ata.Time)) ){
fputs(element->data.Time,c svOut);
//puts("\n",csvOut);
//}
fputs(",",csvOut);
if (ct==0){
prev_total = element->data.BW;
ct=1;
}else {
if(!element->data.isStartO rEnd)//sta rt time
prev_total= prev_total+element->data.B W;
else
prev_total = prev_total-element->data.B W;
}
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);
}
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
//displayList(final);
printf("\n\n**********SORT
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_L
char tempEndTime[MAX_STRING_LEN
float tempStartTimeBitRate;
float tempEndTimeBitRate;
int colNum= 0,nodeCount=0,s_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
memset(tempEndTime,0,MAX_S
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",&tempStart
sscanf(str,"%f",&tempEndTi
}
if (colNum==StartTime) {
//puts(str);
if(15<strlen(str)){
memcpy(tempStartTime,str,s
s_invalid=0;//strcpy(tempS
}
else
s_invalid=1;
}
if (colNum==EndTime){
//puts(str);
if(15<strlen(str)){
memcpy(tempEndTime,str,str
e_invalid=0;//strcpy(tempE
}
else
e_invalid=1;
}
str = strtok(NULL, ",");
}
if(first_line==0){
if((!s_invalid)&&(!tempSta
element = add(element,tempStartTimeB
nodeCount++;
s_invalid=1;
}
if((!e_invalid)&&(!tempEnd
element=add(element,tempEn
nodeCount++;
e_invalid=1;
}
}
memset(tempStartTime,0,MAX
memset(tempEndTime,0,MAX_S
if(first_line==1)
(first_line=0);
colNum=0;
}
fclose (csvIn);
}
printf("\n\n**********LIST
//displayList(element);
element = sortList(element,nodeCount
printf("\n\n**********LIST
//displayList(final);
printf("\n\n**********SORT
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_L
memcpy(temp ->data.Time,Time,strlen(Ti
if(sizeof(temp ->data.Time)>MAX_STRING_LE
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_
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.Tim
{
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->d
puts(element->data.Time);
puts("\t");
printf("%f",element->data.
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
while(element!=NULL){
if((0!=strlen(element->dat
if(MAX_STRING_LEN>(strlen(
fputs(element->data.Time,c
//puts("\n",csvOut);
//}
fputs(",",csvOut);
if (ct==0){
prev_total = element->data.BW;
ct=1;
}else {
if(!element->data.isStartO
prev_total= prev_total+element->data.B
else
prev_total = prev_total-element->data.B
}
BW_Count=prev_total;
//printf("\n%lf",BW_Count)
fprintf(csvOut,"%lf\n",BW_
fputs("\n",csvOut);
//printf("\n");
//puts(element->data.Time)
//printf("\n%lf",BW_Count)
}
}
element=element->next;
}
}
fclose(csvOut);
}
ASKER
I shoudn't checked tempStartTime in this line >> if((!s_invalid)&&(!tempSta rtTime))
That was the problem !!! I changed it and currently running my code for a huge file..
That was the problem !!! I changed it and currently running my code for a huge file..
ASKER
I shoudn't have checked tempStartTime in this line >> if((!s_invalid)&&(!tempSta rtTime))
That was the problem !!! I changed it and currently running my code for a huge file..
That was the problem !!! I changed it and currently running my code for a huge file..
ASKER
Fixed all issues. Everything is fine now :).
Thanks foryour time & help sunnycoder ...
Thanks foryour time & help sunnycoder ...
ASKER
thanks
It should either be i=0 OR i<=nodeCount