Add initial program with LL implementation

This commit is contained in:
stitchy 2023-10-12 14:25:48 -07:00
parent 151ab2977f
commit 367b12484f
Signed by: stitchy
SSH key fingerprint: SHA256:yz2SoxdnY67tfY5Jzb0f2v8f5W3o/IF359kbcquWip8
6 changed files with 183 additions and 0 deletions

84
movies/csv_parser.c Normal file
View file

@ -0,0 +1,84 @@
#include "csv_parser.h"
struct node* parse_csv() {
FILE * file;
file = fopen("movies_sample_1.csv", "r");
// Buffer File For chars
char* buffer;
size_t buff_size = 1000;
size_t chars = -2;
buffer = malloc(buff_size * sizeof(char));
// Deal with Top of CSV
getline(&buffer, &buff_size, file);
chars = getline(&buffer, &buff_size, file);
struct node* head = appendv_node(NULL, parse_line(buffer));
chars = getline(&buffer, &buff_size, file);
// Actually Store the CSV
while(chars != -1) {
appendv_node(head, parse_line(buffer));
chars = getline(&buffer, &buff_size, file);
}
// Free unecessary buffer files
free(buffer);
fclose(file);
return head;
}
struct csv* parse_line(char* line) {
struct csv* mov = malloc(sizeof(struct csv));
char* sub = strtok(line, ",");
char* title = malloc(sizeof(char) *(strlen(sub) + 1));
strcpy(title,sub);
mov->title = title;
mov->year = atoi(strtok(NULL, ","));
sub = strtok(NULL, ",");
char* languages = malloc(sizeof(char) *(strlen(sub) + 1));
strcpy(languages, sub);
mov->languages = languages;
//printf("%s\n", csv->languages);
mov->rating = atof(strtok(NULL, ""));
return mov;
}
void print_movies(struct node* head) {
if(!head) {
printf("You F'd up mate (print_movies)");
return;
}
struct node* node = head;
while(1) {
print_line(node->data);
if(node->node)
node = node->node;
else
return;
}
}
void print_line(struct csv* printer) {
printf("%s, %i, %s, %f\n", printer->title, printer->year, printer->languages, printer->rating);
}

28
movies/csv_parser.h Normal file
View file

@ -0,0 +1,28 @@
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "node.h"
#ifndef CSV_PARSER
#define CSV_PARSER
struct csv {
char* title;
int year;
int numlang;
char* languages;
float rating;
struct node* node;
};
struct node* parse_csv();
struct csv* parse_line(char*);
void print_movies(struct node*);
void print_line(struct csv*);
#endif

12
movies/movies.c Normal file
View file

@ -0,0 +1,12 @@
#include "movies.h"
int main() {
struct node* head = parse_csv();
printf("Test\n");
print_movies(head);
return 0;
}

13
movies/movies.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef MOVIES
#define MOVIES
#include "stdio.h"
#include "stdlib.h"
#include "csv_parser.h"
#include "node.h"
#endif

30
movies/node.c Normal file
View file

@ -0,0 +1,30 @@
#include "node.h"
void append_node(struct node* head, struct node* node_app) {
if(!head) {
head = node_app;
return;
}
struct node* temp = head;
while(temp->node != NULL) {
temp = temp->node;
}
temp->node = node_app;
}
struct node* appendv_node(struct node* head, void* data) {
struct node* node = malloc(sizeof(struct node));
node->data = data;
node->node = NULL;
append_node(head, node);
return node;
}

16
movies/node.h Normal file
View file

@ -0,0 +1,16 @@
#include "stdlib.h"
#ifndef NODES
#define NODES
struct node {
struct node* node;
void* data;
};
void append_node(struct node*, struct node*);
struct node* appendv_node(struct node*, void*);
#endif