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

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...
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 -...
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...
Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. void readFile(Candidate candidates[]) –...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. 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 list. void displayCandidate(Candidate candidates[]) – prints the complete information about the candidate . Candidate First(Candidate candidates[]) – returns single struct element: candidate...
How would I create a nested dictionary given a csv file in Python? Say I want...
How would I create a nested dictionary given a csv file in Python? Say I want to make a dictionary that read {'country':{'China':'Fit', 'China':'Overweight', 'USA': 'Overweight', 'USA': 'Fit', 'England':'Fit'...}, 'category':{'Asian':'Fit', 'Caucasian': 'Overweight', 'Caucasian':'Overweight', 'Asian': 'Fit', 'Middle Eastern': 'Fit'...}} given a file that had country category Weight China Asian Fit China Caucasian Overweight USA Caucasian Overweight USA Asian Fit England Middle Eastern Fit... ... And so on in the file.
What is a csv file and how does database work in general?
What is a csv file and how does database work in general?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT