Question
need help with my C code record_llist.c #include record_llist.h #include #include #include #include NODE *new_node(int val) { NODE *p = (NODE*)malloc(sizeof(NODE)); if(p != NULL) {
need help with my C code
record_llist.c
#include "record_llist.h"
#include
#include
#include
#include
NODE *new_node(int val) {
NODE *p = (NODE*)malloc(sizeof(NODE));
if(p != NULL) {
p->data = val;
p->next = NULL;
return p;
}
}
void display(NODE *start) {
if (start == NULL) return;
NODE *ptr = start;
while (ptr != NULL) {
printf("%d", ptr->data);
ptr = ptr->next;
}
}
NODE* search(NODE *start, char *name) {
NODE *ptr = *start;
while (ptr != start) {
if (strcmp(ptr->data.name, name) == 0)
return ptr;
ptr = ptr->next;
}
return NULL;
}
void insert(NODE **startp, char *name, float score) {
NODE *p = (NODE *) molloc(sizeof(NODE));
strcpy(p->data.name, name);
p->data.score = score;
p->next = NULL;
NODE *prev = NULL;
NODE *ptr = *startp;
NODE *np = startp;
while (np != NULL) {
if(strcmp(ptr->data.name, name) >= 0)
break;
prev = ptr;
ptr = ptr->next;
}
if(prev == NULL) {
*startp = p;
p->next = ptr;
} else {
prev->next = p;
p->next = ptr;
}
}
int delete(NODE **startp, char *name) {
NODE *ptr = *startp;
if (ptr != NULL) {
*startp = ptr->next;
free(ptr);
}
}
void clean(NODE **startp) {
struct node *temp, *np = *startp;
while (np != NULL) {
temp = np;
np = np -> next;
free(temp);
}
*startp = NULL;
}
// the following functions are adapted and modified from previous assignments. You make use of them.
void report_data(NODE *start, char *filename) {
NODE *np = start;
int count = 0;
float mean = 0;
float stddev = 0;
int histogram[6] = {0};
FILE *fp = fopen(filename, "w");
fprintf(fp, "grade report ");
while (np != NULL) {
count++;
mean += np->data.score;
stddev += np->data.score * np->data.score ;
//histogram data
if (np->data.score >=100) histogram[0]++;
else if (np->data.score < 50) histogram[5]++;
else histogram[(unsigned int) (99.99-np->data.score)/10]++;
fprintf(fp, "%-15s%3.1f%4c ", np->data.name, np->data.score, letter_grade(np->data.score));
np = np->next;
}
mean /= count;
stddev = sqrt(stddev/count - mean*mean);
fprintf(fp, " statistics summary ");
fprintf(fp, "%-15s%d ", "count", count);
fprintf(fp, "%-15s%3.1f ", "mean", mean);
fprintf(fp, "%-15s%3.1f ", "stddev", stddev);
fprintf(fp, "%-15s%3.1f ", "median", median(start));
fprintf(fp, "histogram ");
int i;
fprintf(fp, "%-15s%d ", "[90,100]", histogram[0]);
for (i=1; i<5; i++) {
fprintf(fp, "[%d,%d) %d ", 90-i*10, 100-i*10, histogram[i]);
}
fprintf(fp, "%-15s%d ", "[0,50)", histogram[5]);
fclose(fp);
}
void import_data(NODE **startp, char *filename) {
char line[40], name[20];
FILE *fp = fopen(filename, "r");
char *result = NULL;
char delimiters[] = ",";
float score = 0;
if (fp == NULL) {
perror("Error while opening the file. ");
exit(EXIT_FAILURE);
}
while (fgets(line, sizeof(line), fp) != NULL) {
result = strtok(line, delimiters);
strcpy(name, result);
result = strtok(NULL, delimiters);
score = atof(result);
insert(startp, name, score);
}
fclose(fp);
}
char letter_grade(float s){
char r = 'F';
if (s >= 85) r = 'A';
else if (s >= 70) r = 'B';
else if (s >= 60) r = 'C';
else if (s >= 50) r = 'D';
else r = 'F';
return r;
}
void swap(float *first, float *second )
{
float temp = *first;
*first = *second;
*second = temp;
}
void quick_sort(float *a, int left, int right)
{
float key;
int i,j;
if( left < right ) {
key = *(a+left);
i = left+1;
j = right;
while(i <= j) {
while( i <= right && *(a+i) <= key) i++;
while( j >= left && *(a+j) > key ) j--;
if( i < j) swap(a+i, a+j);
}
swap(a+left, a+j);
quick_sort(a, left, j-1);
quick_sort(a, j+1, right);
}
}
float median(NODE *start)
{
if (start == NULL) return 0;
int len = 0, i=0;
NODE *np = start;
while (np) { len++; np = np->next; }
float a[len];
for (i=0, np=start; i
a[i] = np->data.score;
}
quick_sort(a, 0, len-1);
return (len % 2 == 1)? a[len/2] : (a[len/2-1] + a[len/2])/2 ;
}
assignment
- Define singly linked list node structure NODE to hold RECORD data.
- void display(NODE *start); this displays the RECORD data in the linked list.
- NODE *search(NODE *start, char *name); this searches the linked list by the name data.
- void insert(node **startp, char *name, float score); this inserts a record into the linked list at position of dictionary (lexicographical) order of the data.name.
- int delete(NODE **startp, char *name); this deletes a node with RECORD data of the name from the linked list and returns 1 if successful or 0 otherwise.
- void clean(NODE **startp); this cleans the linked list, namely free all nodes.
record_llist.h
#ifndef RECORD_LLIST
#define RECORD_LLIST
#include
#include
#include
#include
/**
* RECORD structure
* name - char array for person's name
* score - float score of record
*/
typedef struct record{
char name[40];
float score;
} RECORD;
/**
* linked list node structure
* data - RECORD data
* next - pointer pointing to next node of linked list
*/
typedef struct node {
int data;
struct node *next;
} NODE;
/**
* Display linked list.
* @param start Pointer to the first node of linked list.
*/
void display(NODE *start);
/**
* Search linked list for name key.
* @param start Pointer to the first node of linked list.
* @param name Key to search
* @return Pointer to found node if found; otherwise NULL
*/
NODE *search(NODE *start, char *name);
/**
* Insert new record to linked list at the sorted position.
* @param startp Pointer pointing to the start pointer of linked list, used to update the start node address in case of change.
* @param name The name data of new record.
* @param score The score data of new record
*/
void insert(NODE **startp, char *name, float score);
/**
* your comment
*/
int delete(NODE **startp, char *name);
/**
* your comment
*/
void clean(NODE **startp);
// The following functions are adapted and/or modified from previous assignment
void import_data(NODE **startp, char *filename);
void report_data(NODE *start, char *filename);
char letter_grade(float score);
void swap(float *first, float *second );
void quick_sort(float *a, int left, int right);
float median(NODE *start);
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started