Question

Write code to read student data from a csv file and add (prepend) that into a linked list. Assume the code is written in only
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the screenshot of code & output:

C:s. Command Prompt 1004, Sarah, 2.7 1001, Mary,3.0 1003, Steve, 2.5 1002, John, 3.5 C:\Users\naman\Desktop\HomeworkLib> sampledata

Below is the C code for the same. The linked list is also printed for testing:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

/** struct definition for linked list node **/

struct node{

int id;

char name[100];

double gpa;

struct node* next;

};

/** function to prepend new node to beginning of linked list and return pointer to the prepended linked list **/

struct node* prepend(struct node* linkedlist,struct node* newNode){

newNode->next=linkedlist;

return newNode;

}

/**function to print linked list**/

void print(struct node* linkedlist){

struct node* temp = linkedlist;

while(temp){

printf("%d,%s,%.1lf\n",temp->id,temp->name,temp->gpa);

temp=temp->next;

}

}

int main(int argc, char* argv[]){

/** if file name not given in command line args **/

if(argc!=2){

printf("USAGE: a <filename>\n");

exit(0);

}

FILE* fp = fopen(argv[1],"r");/** to read from file **/

if(fp){

struct node* linkedlist=NULL;/**pointer to our linked list**/

while(!feof(fp)){

struct node* newNode =(struct node*) malloc(sizeof(struct node));

/**inputting node details**/

fscanf(fp,"%d,%[^,]%*c %lf",&(newNode->id),&(newNode->name),&(newNode->gpa));

linkedlist=prepend(linkedlist,newNode);/**prepending node to linked list**/

}

print(linkedlist);/**printing linked list for testing**/

fclose(fp);

}

else{

/** if file not found **/

printf("File \"%s\" not found!\n",argv[1]);

}

}

Add a comment
Know the answer?
Add Answer to:
Write code to read student data from a csv file and add (prepend) that into a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java code to read from .csv file i currently have a .csv file that looks like...

    Java code to read from .csv file i currently have a .csv file that looks like this: 39.743222, -105.006241, Hospital 39.743981, -105.020017, Home 39.739377, -104.984774, Firehouse 39.627779, -104.839291, McDonald's 39.731919, -104.961814, Chipotle I need to write code to extract the doubles from each line to use for my Linked List. this is what i have so far: Scanner in = new Scanner(new FileInputStream(DATA_FILE));

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.t...

    Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.txt) which you will create from the data provided below. The program will then prompt the user for a name to search for, when it finds the name it will output to a file (call it OFile.txt) the person’s id#, name,...

  • C++ Data Structure Write a program to read a list of students from a file and...

    C++ Data Structure Write a program to read a list of students from a file and create a list. The program should use a linked list for implementation. Each node in the linked list should have the student’s name, a pointer to the next student, and a pointer to a linked list of scores. There may be up to four scores for each student.

  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

  • Trying to figure out what needs to be in the headers.h file. I have written the...

    Trying to figure out what needs to be in the headers.h file. I have written the print function. Qualities in header file main() needs insertionSort() and mergeSortWrapper() Both insertionSort() and mergeSortWrapper() need print(). Please copy-and-paste the following files (0 Points): insertionSort.c /*--------------------------------------------------------------------------* *---- ----* *---- insertionSort.c ----* *---- ----* *---- This file defines a function that implements insertion ----* *---- sort on a linked-list of integers. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* *----...

  • C++ void CLL::push(char data) { // You write - if the size is 0, add a...

    C++ void CLL::push(char data) { // You write - if the size is 0, add a first node in the linked list // by calling addFirst. Otherwise add a new node to the end of the linked list. Note that this // linked list is circular, so you must make the last node's next pointer point to the first node. } void CLL::addFirst(char data) { // you write - add the very first node to the linked list } void...

  • (In C) 8.12 LAB: Warm up: Contacts You will be building a linked list. Make sure...

    (In C) 8.12 LAB: Warm up: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. ContactNode.h - Struct definition, including the data members and related function declarations ContactNode.c - Related function definitions main.c - main() function (2) Build the ContactNode struct per the following specifications: Data members char contactName[50] char contactPhoneNum[50] struct ContactNode* nextNodePtr Related functions CreateContactNode() (2 pt) InsertContactAfter() (2 pts) Insert...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT