Question

In: Computer Science

How to read the given structure from a random CSV file separated by commas(which contains no...

How to read the given structure from a random CSV file separated by commas(which contains no headers only the values of the contents of the structure) and then insert in a binary search tree using one of the structure contents as a key i.e. datetime and handle duplicates in binary search tree by implementing link_list.Please develop a C code for this.

struct data{

char biker_id[200];

char distance_bike_travelled[200];

char datetime[200];

char count_tripr[200];

}

Solutions

Expert Solution


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

struct node
{
char biker_id[200];
char distance_bike_travelled[200];
char datetime[200];
char count_tripr[200];
struct node* left;
struct node* right;
struct node* next;
};
struct node *root = NULL;

struct node* createNode(char bid[200], char dbt[200], char dtime[200], char count[200])
{
struct node* newNode = (node*)malloc(sizeof(struct node));
strcpy(newNode->biker_id, bid);
strcpy(newNode->distance_bike_travelled,dbt);
strcpy(newNode->datetime ,dtime);
strcpy(newNode->count_tripr ,count);
newNode->left = NULL;
newNode->right = NULL;
newNode->next = NULL;
return newNode;
}
  
void createNode1(struct node* root1, char bid[200], char dbt[200], char dtime[200], char count[200])
{
if(root1->next != NULL)
{
while(root1->next != NULL)
root1=root1->next;
}
struct node* newNode1 = (node*)malloc(sizeof(struct node));
strcpy(newNode1->biker_id, bid);
strcpy(newNode1->distance_bike_travelled,dbt);
strcpy(newNode1->datetime ,dtime);
strcpy(newNode1->count_tripr ,count);
newNode1->next = NULL;
newNode1->left = NULL;
newNode1->right = NULL;
root1->next=newNode1;
}
  
struct node* insert(struct node* root1, char bid[200], char dbt[200], char dtime[200], char count[200])
{
if (root1 == NULL) return createNode(bid, dbt, dtime, count);
if (atoi(dtime)<atoi(root1->datetime))
root1->left = insert(root1->left, bid, dbt, dtime, count);
else if (atoi(dtime)>atoi(root1->datetime))
root1->right = insert(root1->right, bid, dbt, dtime, count);
else if (atoi(dtime)==atoi(root1->datetime))
createNode1(root1, bid, dbt, dtime, count);

return root;
}

void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
while(root->next != NULL)
{
printf("%s\t%s\t%s\t%s\n", root->biker_id,root->distance_bike_travelled,root->datetime,root->count_tripr);
root=root->next;
}
inorder(root->right);
}
}
void read_record()
{
char bid[200];
char dbt[200];
char dtime[200];
char count[200];
FILE *fp;
if ((fp = fopen("filename.csv", "r")) == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);   
}
while( fscanf(fp, "%s, %s, %s, %s" , &bid, &dbt, &dtime, &count) != EOF )
{
insert(root, bid, dbt, dtime, count);
}
fclose(fp);
}
int main()
{
read_record();
printf("Bike ID\tDistance Travelled\tDate Time\tCont trip\n");
inorder(root);
return 0;
}

---------------------------------------------------------------------

Please do comment for any further clarifications..


Related Solutions

* readCsvFile() -- Read in a CSV File and return a list of entries in that...
* readCsvFile() -- Read in a CSV File and return a list of entries in that file.    * @param filePath -- Path to file being read in.    * @param classType -- Class of entries being read in.    * @return -- List of entries being returned.    */    public <T> List<T> readCsvFile(String filePath, Class<T> classType){        return null;    } implement this class. Return a list of T type. dont worry about CSV format. Just assume...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? *IMPORTANT* PLEASE READ CAREFULLY....
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? *IMPORTANT* PLEASE READ CAREFULLY. WE HAVE TO DO WHAT THIS ASSIGNMENT DOES OR WE WILL MARKED OFF POINTS. IT DOES NOT HELP WHEN YOU CHANGE THE SKELETON TO YOU'RE PREFERENCE. THIS IS FOR A BASIC C++ LEVEL CLASS SO WE HAVE TO STICK TO BASIC C++ CODE. HOWEVER IT COULD BE WRONG IN TERMS OF WORKING CONDITIONS SO PLEASE HELP FIX THESE. *IMPORTANT* void readFile(Candidate candidates[]) – reads...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? ****IMPORTANT**** PLEASE READ CAREFULLY...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? ****IMPORTANT**** PLEASE READ CAREFULLY ****IMPORTANT**** ***GOALS*** HOW TO CHECK FOR COMMAS, TILL THE END OF FILE. IT WILL CHECK THE LINE FOR THE APPRORIATE FORMAT IN THE TEXT FILE. IF THERE IS MISSING A COMMA, IT WILL IGNORE, IF THERE IS A WHITE SPACE, IT WILL CORRECT AND READ LINE, IF IT IS MISSING 1 OF THE 3 INFORMATION, IT WILL IGNORE. Display candidates’ names using displayList() function...
how to read a csv file in php and make a html table? I can't use...
how to read a csv file in php and make a html table? I can't use the PHP function fgetcsv. I can use explode. I can't put a php inside a php. Acme,Walmart,Ross,BJs,Target,Marshalls,Foot Locker,Giant,Charming Charlie 142,160,28,10,5,3,60,0.28,3167 175,180,18,8,4,1,12,0.43,4033 129,132,13,6,3,1,41,0.33,1471 138,140,17,7,3,1,22,0.46,3204 232,240,25,8,4,3,5,2.05,3613 135,140,18,7,4,3,9,0.57,3028 150,160,20,8,4,3,18,4.00,3131 207,225,22,8,4,2,16,2.22,5158 271,285,30,10,5,2,30,0.53,5702 89,90,10,5,3,1,43,0.30,2054 153,157,22,8,3,3,18,0.38,4127 87,90,16,7,3,1,50,0.65,1445 234,238,25,8,4,2,2,1.61,2087 106,116,20,8,4,1,13,0.22,2818 175,180,22,8,4,2,15,2.06,3917 165,170,17,8,4,2,33,0.46,2220 166,170,23,9,4,2,37,0.27,3498 136,140,19,7,3,1,22,0.63,3607 <!DOCTYPE html> <html> <head>    <meta charset="utf-8">    <title>Stores</title>    <link rel="stylesheet" href="style.css"> </head> <body> <h1>Stores</h1> <?php <table> <tr> <th>Acme</th> <th>Walmart</th> <th>Ross</th> <th>BJs</th> <th>Target</th> <th>Marshalls</th> <th>Foot Locker</th>...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values - Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats - Transform all Upper case characters to Lower case characters - Transform all Lower case characters to Upper case characters - save back the 'repaired' array as csv -...
how to creat Create a text file that contains a list of integer numbers separated by...
how to creat Create a text file that contains a list of integer numbers separated by comma (numbers.txt). E.g. 3, 7, 5, 1, 11, 8,2, 6 2- how to Write a python program (minMax.py) to read the file numbers.txt and find the largest and smallest numbers in the file. (without using use min(), max()). calculate the average and output the results. 3-Use python file i/o, write a python program (fileCopy.py) that makes a copy of minMax.py (Read contents of minMax.cp...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD. void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD. void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking...
Read from a file that contains a paragraph of words. Put all the words in an...
Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with words separated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT